i would like to start a new activity according to the radiobutton that the user choose.this is my non working code(i m using a toast message for now instead of a new activity)
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);
se开发者_运维百科tContentView(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 this is my 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
First, if is not a method.
Second, use
if(rb1.isChecked()){Toast.makeText(cafechoice.this, 1, Toast.LENGTH_SHORT).show();}
if(rb2.isChecked()){Toast.makeText(cafechoice.this, 1, Toast.LENGTH_SHORT).show();}
Without if(v == radio_listener)
becaus v
is not the listener, but the element you pressed on.
EDIT
To avoid the NullPointerException, you should remove final RadioButton
from both button declaration inside the onCreate
method. They are already declared outside of the method.
Also, remove the == true
, but this is just semantic.
You should rather use a 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();
}
};
and in your onCreate
method:
rb1.setOnCheckedChangeListener(radio_listener);
rb2.setOnCheckedChangeListener(radio_listener);
Maybe I'm wrong but I think the problem could be the if clause in the listener:
if(v == radio_listener)
You should use the view given in that function to check if it's your RadioButton
by using an equals()
function or something like that.
Hope you can fix that ;)
精彩评论