2012-07-10 34 views
0

我無法獲取要返回的值。應用程序關閉同意按鈕單擊。我無法從i_am.isChecked()獲取所有其他單選按鈕的值。在Android中無法獲取RadioButton onchecked()的結果

protected void onCreate(Bundle savedInstanceState) { 
    final RadioButton i_am,practic,read_terms; 
    i_am=(RadioButton)findViewById(R.id.first); 
    practice=(RadioButton)findViewById(R.id.second); 
    read_terms=(RadioButton)findViewById(R.id.third); 

    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.termandconditionxml); 

    Button agree_button=(Button)findViewById(R.id.Agree); 
    agree_button.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(checker()) { 
       Toast.makeText(getApplicationContext(), "done all cliked",Toast.LENGTH_LONG).show(); 
      } 
      else { 
// TODO Auto-generated method stub 
       new AlertDialog.Builder(Termsandcondition.this) 
       .setTitle("Error") 
       .setMessage("Please verify ") 
       .setPositiveButton("OK",null).show(); 

      } 
     } 

     private boolean checker() { 
      // TODO Auto-generated method stub 
      System.out.println("iamanhcp"+i_am.isChecked()); 
      if(i_am.isChecked()) { 
       System.out.println("iahcp"+i_am.isChecked()); 
       if(practice.isChecked()) { 
        System.out.println("iama"+practice.isChecked());  
        if(read_terms.isChecked()) {  
         System.out.println("iama"+read_terms.isChecked()); 
         return true;  
        } 
       } 
      } 

      return false; 
     } 


    }); 
}//oncreate end 

============================

07-05 15:36:14.301: W/dalvikvm(1702): threadid=1: thread exiting with uncaught exception (group=0x40014760) 
07-05 15:36:14.332: E/AndroidRuntime(1702): FATAL EXCEPTION: main 
07-05 15:36:14.332: E/AndroidRuntime(1702): java.lang.NullPointerException 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at com.andoid.Termsandcondition$1.checker(Termsandcondition.java:53) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at com.andoid.Termsandcondition$1.onClick(Termsandcondition.java:36) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at android.view.View.performClick(View.java:3110) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at android.view.View$PerformClick.run(View.java:11934) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at android.os.Handler.handleCallback(Handler.java:587) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at android.os.Handler.dispatchMessage(Handler.java:92) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at android.os.Looper.loop(Looper.java:132) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at android.app.ActivityThread.main(ActivityThread.java:4123) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at java.lang.reflect.Method.invoke(Method.java:491) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
07-05 15:36:14.332: E/AndroidRuntime(1702):  at dalvik.system.NativeStart.main(Native Method) 
+1

你能分享來自LogCat的錯誤細節嗎? – 2012-07-10 09:59:14

+0

你可以發佈堆棧跟蹤嗎? – 2012-07-10 09:59:50

回答

0

可能迫使它因爲靠近你您的Toast中的getApplicationContext。改爲使用this。 要從RadioButton中獲取值,請使用OnCheckedChangeListener

Toast.makeText(this, "done all cliked",Toast.LENGTH_LONG).show(); 

i_am.setOnCheckedChangeListener(this); 

希望這有助於你。

相關問題