2011-08-11 3 views
0

나는이 두 활동, Activity_A 및 Activity_B Activity_A은 체크 박스와 버튼이은 previus 활동에서 언급하기

내가 Activity_A 체크 박스 상태 A에 따라 를 시작할 것 Activity_A_button Activity_B를 클릭 할 때 내가 뭘하려고하다 TextView가 표시되거나 사라졌습니다. 여기

문제가 내가 if 문

어떤 아이디어 @ Activity_A 버튼을 클릭 NullPointerExeption를 얻고 있다는 것입니다

Textview1 = (TextView)findViewById(R.id.textView1); 

    if (Main.check1.isChecked()) 

     { 
        //code here 

} 

샘플 코드

 public class Main extends Activity { 
    public static CheckBox check1; 
     private Button Button1; 

CheckBox check1 = (CheckBox)findViewById(R.id.checkBox1); 
Button1 = (Button) findViewById(R.id.button1); 


button1.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 

final Intent i = new Intent(this, Activity_A.class); 

    startActivity(i); 

      } 
     }); 

Activity_B입니까? 미리 감사드립니다.

답변

2

다음과 같이하십시오. 두 번째 활동에서 검색하고 모든 것을 (이 코드는 컴파일되지 않을 것이다) 게시되지 않은 가정 따라

final Intent i = new Intent(this, Activity_A.class); 
    i.putExtras("value",""+check1.isChecked());//sending checked value to next activity 
    startActivity(i); 





Textview1 = (TextView)findViewById(R.id.textView1); 
Bundle bundle = getIntent().getExtras(); 
boolean ok=Boolean.parseBoolean(bundle.getString("value")); 

    if (ok) //checkbox is checked in previous activity 
     { 
       //code here 
     } 
+0

감사합니다 !!!!! 작동 완벽 –

+0

:) 환영합니다 에디 – Rasel

0

를 작동 activity.then를 호출 할 때

체크 박스의 값을 보내처럼 보인다 로컬 변수에

CheckBox check1 = (CheckBox)findViewById(R.id.checkBox1); 

대신 변수 정적 클래스에 CHECK1을 할당

public static CheckBox check1; 

Activity_B에서 참조 할 때 Main.check1은 여전히 ​​null입니다. 그러나 관계없이 UI 요소가 누출 될 때 두 가지 활동간에 상태를 공유하는 것은 여전히 ​​나쁜 방법입니다. 부울 값을 Activity_B을 시작하려는 의도의 일부인 번들로 추가하거나 단추 청취자가 정적 부울 변수를 설정하는 이유는 무엇입니까? 둘 다 훨씬 더 쉽고 쉬운 대안입니다.

0

첫 번째 활동에서 두 번째 활동으로 의도를 전달하여 해결할 것으로 생각됩니다.

final Intent i = new Intent(this, Activity_A.class); 
i.putExtras("value",""+check1.isChecked());//sending checked value to next activity 
startActivity(i);` 

기록 제 활성이이 제 활성

Textview1 = (TextView)findViewById(R.id.textView1); 
Bundle bundle = getIntent().getExtras(); 
boolean ok=Boolean.parseBoolean(bundle.getString("value")); 

if (ok) //checkbox is checked in previous activity 
    { 
      //code here 
    }` 

물품.

+0

도움 주셔서 감사합니다 –

관련 문제