Reputation: 2737
I have a dialog box to be shown with two edittextboxes in it.
When I fill the fields and click on the submit button, I need to save the values in database. I am trying to get the values entered in the fields, but the Null Pointer Exception is raised.
My code is follows
View v = getLayoutInflater().inflate(R.layout.sample, null);
new AlertDialog.Builder(Context)
.setTitle(R.string.Details)
.setView(v)
.setNegativeButton(R.string.Cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
})
.setPositiveButton(R.string.Submit,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
final EditText txtname=(EditText)findViewById(R.id.editText1);
final EditText txtnumber=(EditText)findViewById(R.id.editText2);
Save(txtname.getText().toString(),txtnumber.getText().toString());
}
when I debug and checked, It shows the txtname and txtnumber values as empty. Where did I went wrong? I was using a layout to show the fields
The layout is,
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Name" />
<EditText
android:id="@+id/editText1"
android:layout_width="195dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:ems="10"
android:hint="FirstName LastName"
android:inputType="textPersonName" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Phone" />
<EditText
android:id="@+id/editText2"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:ems="10"
android:inputType="number"
android:maxLength="10" />
</TableRow>
Please help me out to solve this exception!!
Upvotes: 0
Views: 3414
Reputation: 20155
Your txtname
and txtnumber
are null
i.e
final EditText txtname=(EditText)findViewById(R.id.editText1);
final EditText txtnumber=(EditText)findViewById(R.id.editText2);
when you are trying to access null reference you will get NullPointerException
Try to get them like this
final EditText txtname=(EditText)v.findViewById(R.id.editText1);
final EditText txtnumber=(EditText)v.findViewById(R.id.editText2);
Upvotes: 4
Reputation: 39397
findViewById
is relative to the Activity. Your views are not in the activity, but rather in the dialog. You need to call
dialog.findViewById
from this scope.
Upvotes: 1
Reputation: 15973
you need to get the views from the dialod's layout
public void onClick(DialogInterface dialog,
int which) {
final EditText txtname=(EditText)dialog.findViewById(R.id.editText1);
final EditText txtnumber=(EditText)dialog.findViewById(R.id.editText2);
Save(txtname.getText().toString(),txtnumber.getText().toString());
}
Upvotes: 1
Reputation: 10353
Since you haven't provided the full code and a stack trace, I'm taking a wild guess, but I'm assuming it's correct: You're fetching the edit texts using findViewById from the activity's content view and not from the inflated view. Change:
findViewById(R.id.editText1);
findViewById(R.id.editText2);
to
v.findViewById(R.id.editText1);
v.findViewById(R.id.editText1);
Upvotes: 1
Reputation: 10621
I think you are looking at the wrong place. Try using:
final View v = getLayoutInflater().inflate(R.layout.sample, null);
and than
final EditText txtname=(EditText)v.findViewById(R.id.editText1);
final EditText txtnumber=(EditText)v.findViewById(R.id.editText2);
to find the EditText at the "v" view.
Upvotes: 1