我正在尝试从“警告”对话框中的EditText字段中获取字符串,但是当代码运行时,它会一直崩溃,因为在引用edittext字段时它似乎变为空。
从其活动中调用以下方法,该方法将显示一个请求路径的警告对话框
/** get path name */
private void GetPathName() {
AlertDialog.Builder path_name_builder = new AlertDialog.Builder(this);
LayoutInflater path_name_inflater = this.getLayoutInflater();
path_name_builder.setView(path_name_inflater.inflate(R.layout.pathname, null));
path_name_builder.setTitle(R.string.pathnamedialog);
// Add the buttons
path_name_builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
//retrieve path name
EditText path_name_et = (EditText) findViewById(R.id.pathname);
////------> I keep getting a null for path_name_et! crashes after this point
String path_name = path_name_et.getText().toString();
//save data to file
SaveData(path_name);
finish();
}
});
path_name_builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
dialog.cancel();
finish(); //finish activity
}
});
AlertDialog path_name_dialog = path_name_builder.create();
path_name_dialog.show();
}
这是警告对话框的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/pathname"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/pathnamehint"/>
</LinearLayout>
为什么我会为EditText引用获取null的任何想法? 这似乎应该很简单,但我是android和java的新手,所以我正在学习。