如何使用带有单选按钮的if语句

时间:2011-05-02 16:52:19

标签: android

我想根据用户选择的单选按钮开始新的活动。这是我的非工作代码(我现在使用的是吐司消息而不是新活动)

public class cafechoice  extends Activity {   

    /** Called when the activity is first created. */
public RadioButton rb1;
public RadioButton rb2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.cafechoice);

        final RadioButton rb1 = (RadioButton) findViewById(R.id.radio_red);
        final RadioButton rb2 = (RadioButton) findViewById(R.id.radio_blue);
        rb1.setOnClickListener(radio_listener);
        rb2.setOnClickListener(radio_listener);

}
    OnClickListener radio_listener = new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
             if(v == radio_listener)
            {

            if(rb1.isChecked() == true){Toast.makeText(cafechoice.this, 1, Toast.LENGTH_SHORT).show();}
            if(rb2.isChecked() == true){Toast.makeText(cafechoice.this, 1, Toast.LENGTH_SHORT).show();}
            }
    }
    };

}

@rekaszeru这是我的logcatview

05-02 17:16:28.206: WARN/dalvikvm(678): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-02 17:16:28.216: ERROR/AndroidRuntime(678): Uncaught handler: thread main exiting due to uncaught exception
05-02 17:16:28.248: ERROR/AndroidRuntime(678): java.lang.RuntimeException: Unable to start activity ComponentInfo{kostas.menu.chania/kostas.menu.chania.cafechoice}: java.lang.NullPointerException
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2497)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at android.os.Looper.loop(Looper.java:123)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at android.app.ActivityThread.main(ActivityThread.java:4338)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at java.lang.reflect.Method.invoke(Method.java:521)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at dalvik.system.NativeStart.main(Native Method)
05-02 17:16:28.248: ERROR/AndroidRuntime(678): Caused by: java.lang.NullPointerException
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at kostas.menu.chania.cafechoice.onCreate(cafechoice.java:24)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444)
05-02 17:16:28.248: ERROR/AndroidRuntime(678):     ... 11 more

3 个答案:

答案 0 :(得分:1)

首先,如果不是方法。

其次,使用

        if(rb1.isChecked()){Toast.makeText(cafechoice.this, 1, Toast.LENGTH_SHORT).show();}
        if(rb2.isChecked()){Toast.makeText(cafechoice.this, 1, Toast.LENGTH_SHORT).show();}

没有if(v == radio_listener)因为v不是听众,而是你按下的元素。

修改

要避免NullPointerException,您应该从final RadioButton方法中的两个按钮声明中删除onCreate。它们已在方法之外声明。

此外,删除== true,但这只是语义。

答案 1 :(得分:0)

您应该使用checkedChangeListener:

final CompoundButton.OnCheckedChangeListener radio_listener = 
    new CompoundButton.OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if (!isChecked)
            return;
        if (buttonView.getId() == R.id.radio_red)
            Toast.makeText(cafechoice.this, 1, Toast.LENGTH_SHORT).show();
        else if (buttonView.getId() == R.id.radio_blue)
            Toast.makeText(cafechoice.this, 1, Toast.LENGTH_SHORT).show();
    }
};

并在您的onCreate方法中:

rb1.setOnCheckedChangeListener(radio_listener);
rb2.setOnCheckedChangeListener(radio_listener);

答案 2 :(得分:0)

也许我错了,但我认为问题可能是听众中的if子句:

if(v == radio_listener)

您应该使用该函数中给出的视图,通过使用RadioButton函数或类似内容来检查它是否是您的equals()

希望你能解决这个问题;)