关闭AlertDialog时生成NPE

时间:2013-01-21 16:51:46

标签: android nullpointerexception android-edittext android-alertdialog

我的活动开始时会弹出警告框。它显示一个自定义视图,其中包含2个文本框和2个edittext视图。出于某种原因,当我点击确认关闭对话框并创建一个新对象时,我会得到一个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上的java.lang.NullPointerException $ 2.onClick(MainActivity.java:90)

LOG CAT:

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());

1 个答案:

答案 0 :(得分:1)

只需将MainActivity.this.findViewById替换为v.findViewById,它就可以正常使用