当我的活动开始时,我会弹出一个警报框。它显示一个由 2 个文本框和 2 个编辑文本视图组成的自定义视图。出于某种原因,当我单击确认关闭对话框并使用在这些字段中输入的值创建一个新对象时,我得到一个 NPE。我不确定为什么。下面是我创建对话框的方法:
public Dialog createDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View v = getLayoutInflater().inflate(R.layout.order_dialog, null);
builder.setView(v);
final EditText tableNum = (EditText)MainActivity.this.findViewById(R.id.numberEntry);
final EditText coverNum = (EditText)MainActivity.this.findViewById(R.id.coversEntry);
builder.setMessage("Order Information");
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Create order object in here
table = Integer.parseInt(tableNum.getText().toString());
covers = Integer.parseInt(coverNum.getText().toString());
order = new Order(covers,table);
Toast.makeText(MainActivity.this, "order created!", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "order: " + "table: " + order.getTable() + " covers: " + order.getCovers(), Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
这是我得到的错误。错误:
com.example.waitron5.MainActivity$2.onClick(MainActivity.java:90) 处的 java.lang.NullPointerException
日志猫:
FATAL EXCEPTION: main
01-21 16:46:31.583: E/AndroidRuntime(11983): java.lang.NullPointerException
01-21 16:46:31.583: E/AndroidRuntime(11983): at com.example.waitron5.MainActivity$2.onClick(MainActivity.java:87)
01-21 16:46:31.583: E/AndroidRuntime(11983): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
01-21 16:46:31.583: E/AndroidRuntime(11983): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 16:46:31.583: E/AndroidRuntime(11983): at android.os.Looper.loop(Looper.java:137)
01-21 16:46:31.583: E/AndroidRuntime(11983): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-21 16:46:31.583: E/AndroidRuntime(11983): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 16:46:31.583: E/AndroidRuntime(11983): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 16:46:31.583: E/AndroidRuntime(11983): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-21 16:46:31.583: E/AndroidRuntime(11983): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-21 16:46:31.583: E/AndroidRuntime(11983): at dalvik.system.NativeStart.main(Native Method)
这是我的 order_dialog.xml 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<!-- Table Number -->
<TextView
android:id="@+id/tablelabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table Number: " />
<EditText
android:id="@+id/numberEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:width="40dp" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<!-- Covers -->
<TextView
android:id="@+id/coverslabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Covers: " />
<EditText
android:id="@+id/coversEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:width="40dp" />
</LinearLayout>
第 89 行出现错误:
table = Integer.parseInt(tableNum.getText().toString());