Android 为什么从自定义对话框获取EditText内容时出错?
我有一个自定义对话框,其中有一个EditText和按钮。在我的对话框中性按钮的onClick方法中,我想根据在EditText中键入的内容生成一个新字符串 当按下菜单按钮时,我正在做的是给菜单充气。菜单按钮显示带有2个选项的警报对话框。在AlertDialog中选择一个选项后,将创建另一个选项,其中包含一个EditText,要求输入名称。当我尝试“String name=nameOfEditText.getText().toString();”时,会出现空指针错误 这是密码Android 为什么从自定义对话框获取EditText内容时出错?,android,android-edittext,Android,Android Edittext,我有一个自定义对话框,其中有一个EditText和按钮。在我的对话框中性按钮的onClick方法中,我想根据在EditText中键入的内容生成一个新字符串 当按下菜单按钮时,我正在做的是给菜单充气。菜单按钮显示带有2个选项的警报对话框。在AlertDialog中选择一个选项后,将创建另一个选项,其中包含一个EditText,要求输入名称。当我尝试“String name=nameOfEditText.getText().toString();”时,会出现空指针错误 这是密码 // cre
// 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()方法。NameOfCaleditText是包含这些方法的类的本地EditText
private final class AddOnClickListener implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
String s = nameOfLocalEditText.getText().toString();
}
}
有人能告诉我为什么我的应用程序force会在生成字符串s时关闭吗
这是自定义_对话框的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>
同[本问题][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而不是充气布局应该可以工作。
现在我的首选是使用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);
}
}
在
mDialog.show()之后定义EditText代码>语句。并使用对话框对象查找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();
}
});
我试过了。我还是会出错。我觉得这可能与我计划的其他部分有关。我有一个listactivity,其中包含我为列表制作的类和适配器。填充列表后,由于某种原因,对话框将不会出现。但是现在列表是空的,您能显示日志中的错误吗?我想你可能忘了addView,我已经更新了我的答案。我的自定义布局中有一个按钮,它响应这个类中的一个方法。当在对话框中单击该按钮时,它将强制关闭。我更希望使用AlertDialog设置和中性按钮,因为它看起来更好。我认为即使使用常规对话框,使用getText()时仍然会出现错误methods@akonwi你能发布你放在这个对话框中的源代码吗?你把它放在listview的onClick事件中了吗?谢谢。现在我的首选是使用AlertDialog,这样我就可以使用中性按钮。我的自定义对话框中已经有一个按钮,可以发送给用户查找文件。我希望中性按钮从AlertDialog组件获取数据
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();
}
});