就是在使用自定义AlbertDialog时,无法读取到写入对话框的数据,请各位大神求助,谢谢
出现的异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.ztz.myapp.MainActivity$8.onClick(MainActivity.java:358)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5290)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
出现异常的位置:
private void distView(final int selectedPosition)
{
//装载app\src\main\res\layout\receive_dialog.xml界面布局文件
TableLayout layoutForm = layoutForm = (TableLayout)getLayoutInflater()
.inflate(R.layout.receive_dialog, null);
new AlertDialog.Builder(this)
//设置对话框的标题
.setTitle("对话框")
//设置对话框显示的view对象
.setView(layoutForm)
//为对话框设置一个“确定”的按钮
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String disName = ((EditText)findViewById(R.id.disDate)).getText().toString();
String disDate = ((EditText)findViewById(R.id.disDate)).getText().toString();
String disBatchNum = ((EditText)findViewById(R.id.disBatchNum)).getText().toString();
//这三个String都无法读取出来
Toast.makeText(getApplicationContext(), disName + disDate + disBatchNum,Toast.LENGTH_SHORT).show();
}
})
//为对话框设置一个“取消”按钮
.setNegativeButton("取消", null)
//创建并显示对话框
.create()
.show();
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/receiveDialog"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10pt"
android:text="货品名称:"/>
<!--输入货品名称的文本框-->
<EditText
android:id="@+id/disName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写货品名称" />
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="进货日期:"
android:textSize="10pt"/>
<!--输入进货日期的文本框-->
<EditText
android:id="@+id/disDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写进货日期"
android:selectAllOnFocus="true"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10pt"
android:text="进货批次号:"/>
<!--输入进货批次号的文本框-->
<EditText
android:id="@+id/disBatchNum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入进货批次号"/>
</TableRow>
</TableLayout>