(문자열을 비교할 수 있도록 편집 됨) 하나의 TextView와 2 개의 버튼이 있으며, 하나의 버튼은 "Month"다른 버튼 "Week"입니다. 누를 때마다 Textview를 변경하려고합니다. Month, Week 예 : 처음 활동을 시작하면 예상대로 "월"이 표시됩니다.android에서 버튼을 누른 후 값을 활동으로 변경하는 방법은 무엇입니까?
"Week"버튼을 누르면 TextView에 항상 "Week"가 표시되고, "Month"버튼을 클릭해도 여전히 주간보기가 표시됩니다.
디버깅을 말한다 내가 누를 때 "월", "월"= "true"로, 가치 문
if (extras != null){ // <-------here is still true
month = extras.getString(month); // <-------here is false
}
이 갑자기가는 경우 값은 여전히 "참"하지만 1에 다음에서 onCreate "false"
나는 settext를 단추로 사용할 수 있지만 나중에 데이터를 표시하는 그래프를 추가하므로 onCreate를 수행하고 싶습니다. 뷰를 생성 할 때마다 (문자열을 비교하여) 어떤 뷰가 선택되었는지 확인하고 메시지와 그래프를 표시합니다. 처음으로 달보기를 표시합니다.
내가 뭘 잘못하고 있니?
당신은 ==
을 사용하여 Java에서 문자열을 비교할 수 없습니다
package com.isma.report;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class report1 extends Activity{
TextView viewtime;
String month = "true";
Intent starterIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
starterIntent = getIntent();
Bundle extras = getIntent().getExtras();
if (extras != null) {
month = extras.getString("month");
}
// Set the text
viewtime = (TextView) findViewById(R.id.StudentReportText);
if (month.equals("true")){
viewtime.setText("Monthly View");
}
if{month.equals("false")){
viewtime.setText("Weekly View");
}
//Month View Button
Button bMonth = (Button) findViewById(R.id.monthincome);
bMonth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
starterIntent.putExtra(month, "true");
startActivity(starterIntent);
finish();
}
});
//Week View Button
Button bWeek = (Button) findViewById(R.id.weekincome);
bWeek.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
starterIntent.putExtra(month, "false");
startActivity(starterIntent);
finish();
}
});
}
}
당신이 아이디의 (monthincome 및 monthincome)입니다 버튼 – Siva
가 monthincome을하고 안드로이드를 weekincome 확인할 수 있습니다 ID를 = "@ + ID/monthincome" 안드로이드 : ID = "@ + ID/weekincome " – Isma
일부는 액세스 객체가 null 인 경우 디버그하고 null 객체를 가져 오는 위치를 찾습니다. – Siva