我有一个带有EditText和Button的自定义对话框。在我的对话框中性按钮的onClick方法中,我想从已输入EditText的内容中创建一个新字符串。
当我按下菜单按钮时,我正在做的是膨胀菜单。菜单按钮显示AlertDialogs有2个选项。当在AlertDialog中选择一个选项时,会创建另一个选项,其中有一个EditText,要求输入名称。当我尝试“String name = nameOfEditText.getText()。toString();”
时出现空指针错误这是代码
// 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);
}
});
现在这里是AddOnClickListener()方法。 nameOfLocalEditText是包含这些方法的类的本地EditText。
private final class AddOnClickListener implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
String s = nameOfLocalEditText.getText().toString();
}
}
有谁可以告诉我为什么我的app力量在制作String s时会关闭?
这是custom_dialog
的xml <?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>
这是logcat错误消息
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)
答案 0 :(得分:1)
在mDialog.show();
语句后定义EditText。并使用您的Dialog对象查找您的EditText ID,如下所示:
mBuilder.setView(layout);
Dialog mDialog = mBuilder.create();
mDialog.show();
nameOfLocalEditText = (EditText) mDialog.findViewById(R.id.name);
现在您应该能够使用EditText。
您可以像我在项目中使用的那样使用此代码而不是警报对话框:
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();
}
});
答案 1 :(得分:0)
同样的[这个问题] [1]
试试这个
nameOfLocalEditText = (EditText)layout.findViewById(R.id.name);
layout.addView(nameOfLocalEditText);
更新:参考[此链接] [2],您可以尝试
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
使用dialog.setContentView代替inflate布局应该有效。
现在我的偏好是使用AlertDialog,所以我可以使用 中性按钮。我的自定义对话框中已经有一个按钮要发送 用户查找文件。我想要中性按钮来获取数据 来自AlertDialog组件
此代码对我有用:
// 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);
}
}