2012-04-20 43 views
0

誰能幫我這個,告訴我,我做了錯誤...當我嘗試用單選按鈕的Eclipse做任何事情我拋出該異常Android的 - 空指針異常的對話與單選按鈕

threadid=1: thread exiting with uncaught exception (group=0x40015560) 
FATAL EXCEPTION: main 
java.lang.NullPointerException 
cz.nasdaq.RbtnActivity$1.onClick(RbtnActivity.java:36) 
android.view.View.performClick(View.java:2485) 
android.view.View$PerformClick.run(View.java:9080) 
android.os.Handler.handleCallback(Handler.java:587) 
android.os.Handler.dispatchMessage(Handler.java:92) 
android.os.Looper.loop(Looper.java:123) 
. 
. 
. 

與這個代碼

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button btn = (Button)findViewById(R.id.btn1);   
     btn.setOnClickListener(new OnClickListener() { 

      public void onClick(View arg0) { 
       final Dialog dialog = new Dialog(RbtnActivity.this); 
       dialog.setContentView(R.layout.dlg); 
       dialog.show(); 

       RadioButton drb0=(RadioButton)findViewById(R.id.DialogRb0); 
       drb0.setChecked(true); 
+0

什麼是第36行? – Femaref 2012-04-20 16:41:35

+0

是主佈局中的單選按鈕? – 2012-04-20 16:45:35

回答

2
RadioButton drb0=(RadioButton)findViewById(R.id.DialogRb0); 
drb0.setChecked(true); 

應該

RadioButton drb0=(RadioButton)dialog.findViewById(R.id.DialogRb0); 
drb0.setChecked(true); 

請注意對話框 .findViewById(R.id.DialogRb0);

當您在對話框的佈局中進行搜索時,您正在主要佈局中搜索對話框RadioButton。當你搜索它時,你的變量drb0爲null,當你調用setChecked(true)時,它將導致空指針異常。