Reputation: 17139
I'm working with a RadioGroup
. I have used the following code to implement a RadioButton
inside an AlertDialog
.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
int checkedRadioButton = 0;
try {
checkedRadioButton = radioGroup.getCheckedRadioButtonId();
} catch (Exception e) {
e.printStackTrace();
}
int i =0;
switch (checkedRadioButton) {
case R.id.a2s :
datasource.updateIcon(i,itemid);
break;
case R.id.android: i=1;
datasource.updateIcon(i,itemid);
break;
But the checkedRadioButton = radioGroup.getCheckedRadioButtonId();
throws a NullPonterException when called. Why is this happening?
02-29 07:30:30.514: W/System.err(13957): java.lang.NullPointerException
02-29 07:30:30.514: W/System.err(13957): at com.manager.boot.r1223.OSListActivity$5.onClick(OSListActivity.java:219)
02-29 07:30:30.514: W/System.err(13957): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
02-29 07:30:30.514: W/System.err(13957): at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 07:30:30.514: W/System.err(13957): at android.os.Looper.loop(Looper.java:137)
02-29 07:30:30.514: W/System.err(13957): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-29 07:30:30.514: W/System.err(13957): at java.lang.reflect.Method.invokeNative(Native Method)
02-29 07:30:30.514: W/System.err(13957): at java.lang.reflect.Method.invoke(Method.java:511)
02-29 07:30:30.514: W/System.err(13957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-29 07:30:30.514: W/System.err(13957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-29 07:30:30.514: W/System.err(13957): at dalvik.system.NativeStart.main(Native Method)
Layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<RadioButton
android:id="@+id/a2s"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Argen2Stone ROM" />
</RadioGroup>
</LinearLayout>
Upvotes: 0
Views: 471
Reputation: 5177
the error happen here: RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
your AlertDialog has using setContentView
to the layout or not ?
Upvotes: 2
Reputation: 6018
You should double check your layout. Either R.id.radiogroup
does not exist in the current view you are looking at, or it is not of type RadioGroup
.
Upvotes: 0