2014-01-24 250 views
0

我使用單選按鈕進行選擇。 當我把setOnCheckedChangeListener的應用程序crach。 請幫忙。Android單選按鈕選擇

public class SetReseau extends Activity{ 

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    RadioGroup radioReseau = (RadioGroup) findViewById(R.id.radioReseau); 
    setContentView(R.layout.set_reseau_setting); 

    radioReseau.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      RadioButton radioButton = (RadioButton) findViewById(checkedId); 
      Toast.makeText(getApplicationContext(), "" + radioButton.getText(), Toast.LENGTH_LONG).show(); 
     } 
    }); 

還有就是我的logcat

E/AndroidRuntime(18822): Uncaught handler: thread main exiting due to uncaught exception 
E/AndroidRuntime(18822): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.sms/com.sms.SettingsActivity}: 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sms/com.sms.SetReseau}: java.lang.NullPointerException 

...

回答

5

你需要扭轉這種

RadioGroup radioReseau = (RadioGroup) findViewById(R.id.radioReseau); 
setContentView(R.layout.set_reseau_setting); 

因此改變

setContentView(R.layout.set_reseau_setting); 
RadioGroup radioReseau = (RadioGroup) findViewById(R.id.radioReseau); 

您需要首先對佈局進行膨脹,然後初始化視圖,findViewById將查找當前鑲嵌佈局中的id。

+1

非常感謝。這行得通。我看到了錯誤。謝謝 – Andrey