2014-04-18 2 views
0

(문자열을 비교할 수 있도록 편집 됨) 하나의 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(); 

      } 
     }); 
    } 
} 
+0

당신이 아이디의 (monthincome 및 monthincome)입니다 버튼 – Siva

+0

가 monthincome을하고 안드로이드를 weekincome 확인할 수 있습니다 ID를 = "@ + ID/monthincome" 안드로이드 : ID = "@ + ID/weekincome " – Isma

+0

일부는 액세스 객체가 null 인 경우 디버그하고 null 객체를 가져 오는 위치를 찾습니다. – Siva

답변

0

마침내 나는 그것을 고칠 수있었습니다!

처음으로 모든 덕분에 도움이되었습니다.

이제 수정 사항이 매우 간단합니다.

String 달의 초기화를 값없이 onCreate 메소드 내부로 옮겼습니다. 전진 시간이 실행 ​​중이거나 아무 버튼도 누르지 않으면 if 문을 확장하여 true 값을 할당합니다.

String month = ""; 

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
    month = extras.getString(month); 

    } 
    else{ 
     month = "true"; 
    } 

나는 또한 어떤 값으로 초기화 온 클릭 방법

String month = ""; 

매우 간단의 달 변수하지만 알아 내기 위해 이일 걸렸다! ; P 덕분에 다시 사람이

2
if (month == "true") 

을 Heres 코드. equals(...) - 예 ...

if (month.equals("true")) 
+0

그 줄에서 다음과 같이 충돌합니다 : if (month.equals ("true")) – Isma

+0

왜 잘못된 값을 얻는가? 내가 사실로 초기화한다면? 어떤 버튼을 누르면 false 값을 전달합니까? – Isma

+0

포인트는'if (month == "true")'는 결코 참된 결과로 평가되지 않고 항상 실행되는'else' 블록을가집니다. 'equals (...) '를 사용하여 충돌하는 경우 코드에 다른 문제가 있습니다.이 경우 질문을 편집하고 logcat을 게시하십시오. – Squonk

관련 문제