2014-01-24 40 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