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)
The same [this question][1]
Try this
Update: refer to [this link][2] you can try
Use dialog.setContentView instead of inflate layout should work.
This code worked for me: