【问题标题】:Android Radio Button selectionAndroid单选按钮选择
【发布时间】:2014-01-24 17:58:08
【问题描述】:

我使用单选按钮进行选择。 当我将 setOnCheckedChangeListener 放入应用程序时。 请帮忙。

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(); 
        }
    });

这是我的日志猫

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

...

【问题讨论】:

    标签: android radio-button selection


    【解决方案1】:

    你需要扭转这个

     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 的视图。

    【讨论】:

    • 非常感谢。这行得通。我看到了错误。谢谢
    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    相关资源
    最近更新 更多