Reputation: 7
I have a custom dialog with an EditText and Button in it. In my dialog neutral button's onClick method, I want to make a new string from what has been typed into the EditText.
What I am doing is inflating a menu when the menu button is pressed. The menu buttons show AlertDialogs with 2 options. When an option is selected in the AlertDialog, another one is created which has an EditText in it asking for a name. The null pointer error comes when I attempt "String name = nameOfEditText.getText().toString();"
here is the code
// creation of the dialog
Builder mBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout= inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.RelativeLayout1));
mBuilder.setNeutralButton("Add", new AddOnClickListener());
mBuilder.setView(layout);
Dialog mDialog = mBuilder.create();
mDialog.show();
nameOfLocalEditText = (EditText) findViewById(R.id.name);
Button dialogButton = (Button) mDialog.findViewById(R.id.button1);
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent imp = new Intent();
imp.setType("file/*");
imp.setAction(Intent.ACTION_GET_CONTENT);
imp.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(imp, 0);
}
});
Now here is the AddOnClickListener() method. nameOfLocalEditText is an EditText local to the class containing these methods.
private final class AddOnClickListener implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
String s = nameOfLocalEditText.getText().toString();
}
}
Can anyone tell me why my app force closes at the point of making the String s?
This is the xml for custom_dialog
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Get File" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
This is the logcat error message
05-24 23:27:27.166: E/AndroidRuntime(595): FATAL EXCEPTION: main
05-24 23:27:27.166: E/AndroidRuntime(595): java.lang.NullPointerException
05-24 23:27:27.166: E/AndroidRuntime(595): at com.akonwi.listSyllabi.ListSyllabiActivity$AddOnClickListener.onClick(ListSyllabiActivity.ja va:145)
05-24 23:27:27.166: E/AndroidRuntime(595): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
05-24 23:27:27.166: E/AndroidRuntime(595): at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 23:27:27.166: E/AndroidRuntime(595): at android.os.Looper.loop(Looper.java:137)
05-24 23:27:27.166: E/AndroidRuntime(595): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-24 23:27:27.166: E/AndroidRuntime(595): at java.lang.reflect.Method.invokeNative(Native Method)
05-24 23:27:27.166: E/AndroidRuntime(595): at java.lang.reflect.Method.invoke(Method.java:511)
05-24 23:27:27.166: E/AndroidRuntime(595): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-24 23:27:27.166: E/AndroidRuntime(595): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-24 23:27:27.166: E/AndroidRuntime(595): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 734
Reputation: 9827
Define EditText after your mDialog.show();
statement. and use your Dialog object to find your EditText id like below :
mBuilder.setView(layout);
Dialog mDialog = mBuilder.create();
mDialog.show();
nameOfLocalEditText = (EditText) mDialog.findViewById(R.id.name);
Now you should be able to use EditText.
Instead of Alert Dialog you can use this Code as I used in my project :
Dialog myDialog;
myDialog = new Dialog(Activity_ShoppingList.this);
myDialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
myDialog.setContentView(R.layout.custom_dialog);
myDialog.setTitle("Endre/Slett ingrediens");
myDialog.setCancelable(true);
myDialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.edit);
myDialog.show();
edtName = (EditText)myDialog.findViewById(R.id.edtname);
edtqty = (EditText)myDialog.findViewById(R.id.edtqty);
edtType = (EditText)myDialog.findViewById(R.id.edtType);
edtName.setText(rName);
edtqty.setText(rAmm);
edtType.setText(rType);
btnNew = (Button) myDialog.findViewById(R.id.btnDelete);
btnNew.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myDbHelper.DeleteSelDataBase(rName);
new AsyncButtonClk().execute();
myDialog.dismiss();
//Toast.makeText(getApplicationContext(), "Row Id : " + Global.shopping_name.get(ARG2) , Toast.LENGTH_SHORT).show();
}
});
Upvotes: 1
Reputation: 10193
The same [this question][1]
Try this
nameOfLocalEditText = (EditText)layout.findViewById(R.id.name);
layout.addView(nameOfLocalEditText);
Update: refer to [this link][2] you can try
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
Use dialog.setContentView instead of inflate layout should work.
Right now my preference is to use the AlertDialog so I can have the neutral button. I already have a button in my custom dialog to send the user to find a file. I want the neutral button to take the data from the AlertDialog components
This code worked for me:
// creation of the dialog
Builder mBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.RelativeLayout1));
mBuilder.setNeutralButton("Add", new AddOnClickListener());
mBuilder.setView(layout);
Dialog mDialog = mBuilder.create();
mDialog.show();
nameOfLocalEditText = (EditText) layout.findViewById(R.id.editText1);
Button dialogButton = (Button) layout.findViewById(R.id.button1);
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent imp = new Intent();
imp.setType("file/*");
imp.setAction(Intent.ACTION_GET_CONTENT);
imp.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(imp, 0);
}
});
private final class AddOnClickListener implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
String s = nameOfLocalEditText.getText().toString();
Log.d(TAG, s);
}
}
Upvotes: 0