Reputation: 97
Hi I have written simple custom dialog . which has few check boxs and one submitt button .
whenever I tried to read the checkbox apllication throws Nullpointer exception .. can somebody helps to solve this , below is my custom dailog code
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
CheckBox chk1= (CheckBox) findViewById(R.id.chkbox1);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(chk1.isChecked())
dialog.dismiss();
}
});
dialog.show();
}
Upvotes: 1
Views: 2929
Reputation: 97
I am trying add radio group to the custom dialog .. It comes when dialog is loaded , but how to add the action lister to that radio group in the dialog .. below is my custom layout xml .
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="@android:style/Theme.Light">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
/>
<CheckBox
android:id="@+id/chksmart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMARTAPPLIANCE "
android:layout_below="@+id/editText"
/>
<CheckBox
android:id="@+id/meter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Device"
android:layout_below="@+id/chksmart"
/>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/android:list"
android:textColor="@android:color/black" >
<RadioButton
android:id="@+id/radioGet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="GET"
android:textColor="@android:color/background_dark" />
<RadioButton
android:id="@+id/radioPut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="PUT"
android:textColor="@android:color/background_dark" />
<RadioButton
android:id="@+id/radioPost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="POST"
android:textColor="@android:color/background_dark" />
<RadioButton
android:id="@+id/radioDelete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="DELETE"
android:textColor="@android:color/background_dark" />
<RadioButton
android:id="@+id/radioevent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="radioClickHandler1"
android:text="ADDEVENT"
android:textColor="@android:color/background_dark" />
</RadioGroup>
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" SUBMIT "
android:textColor="#00000f"
android:textSize="25px"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/radiogroup"
/>
Upvotes: 0
Reputation: 7636
NullPointerException
because you didn't instantiated with dialog.findViewById()
and set OnClickListener
for the CheckBox. Place it as below:
CheckBox chk1= (CheckBox)dialog.findViewById(R.id.chkbox1);
chk1.setOnClickListener(new OnClickListener() {
//do something here
});
Upvotes: 1
Reputation: 476
When you have populated a layout for dialog then you need to access it through dialog. But you are accessing it through parent view. Anyway just call it through dialog.findViewById(R.id.chkbox1)
Upvotes: 1
Reputation: 6867
Change:
CheckBox chk1= (CheckBox) findViewById(R.id.chkbox1);
to:
CheckBox chk1= (CheckBox) dialog.findViewById(R.id.chkbox1);
Remember that if you're simply using findViewById()
, you're calling it for Activity
in which you currently are, but as far as I see, you want to find this CheckBox
in R.layout.custom
which is set for dialog
.
I see that you're properly loading dialogButton
, so you probably just forgot to do the same with chk1
.
Upvotes: 1