Android:警报对话框崩溃

时间:2013-06-16 08:39:17

标签: android android-listview nullpointerexception android-alertdialog

我有一个警报对话框,在点击ListView项目时弹出。警报对话框具有包含两个EditTexts和TextView的自定义布局。但是,在单击对话框上的“确定”按钮时调用EditText.getText()时,应用程序将与java.lang.NullPointerException崩溃。请帮我调试一下。

listview onClickListener代码:

lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
            Cursor cursor = (Cursor) listView.getItemAtPosition(position);
            String cn = cursor.getString(cursor.getColumnIndex("CourseName"));
            //Toast.makeText(getApplicationContext(), "Selected: "+cn, Toast.LENGTH_SHORT).show();
            LayoutInflater lf = LayoutInflater.from(List_of_Courses.this);
            final View DialogView = lf.inflate(R.layout.dialog, null);

            final EditText input1 = (EditText) findViewById(R.id.attendanceet);
            final EditText input2 = (EditText) findViewById(R.id.totalclasseset);

            final AlertDialog.Builder alert = new AlertDialog.Builder(List_of_Courses.this);
            alert.setTitle(cn).setView(DialogView).setPositiveButton("OK", 
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int whichbutton) {
                            Log.v("Test","We're checking");
                            input1.getText();
                            input2.getText();
                            Log.v("Test","We're good");
                            Log.v("Dialog", input1.getText().toString());
                            Log.v("Dialog", input2.getText().toString());
                        }
                    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int whichbutton) {
                            //User clicked cancel so doing nothing.
                            Log.v("CancelDialog", "User clicked Calcel");
                        }
                    });
            alert.show();
        }
    });

dialog.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="horizontal"
    android:id="@+id/DialogLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:paddingLeft="25sp" 
    android:paddingRight="25sp" >

    <EditText
        android:id="@+id/attendanceet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_weight="1"
        android:inputType="number" >
    </EditText>


    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/tvLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:paddingTop="6sp" >


        <TextView 
            android:id="@+id/outof"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/"
            android:textSize="20sp"
            android:paddingLeft="15sp"
            android:paddingRight="15sp"
            android:textColor="#FFFFFF" >
        </TextView>


    </LinearLayout>


    <EditText
        android:id="@+id/totalclasseset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_weight="1"
        android:inputType="number" >
    </EditText>

</LinearLayout>

</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

您应该使用对话框对象将input1初始化为输入2。

final View DialogView = lf.inflate(R.layout.dialog, null);
final EditText input1 = (EditText) DialogView.findViewById(R.id.attendanceet);

您可以找到设置为活动的当前视图层次结构的ViewById。在你的情况下,你膨胀一个对话框,你当前的视图是列表视图项目点击对话框。因此,您应该使用对话框对象来初始化视图。

您可以删除以下

的最终修改器
 AlertDialog.Builder alert = new AlertDialog.Builder(List_of_Courses.this);   

答案 1 :(得分:0)

final EditText input1 = (EditText) findViewById(R.id.attendanceet);
final EditText input2 = (EditText) findViewById(R.id.totalclasseset);

应该是

final EditText input1 = (EditText) DialogView.findViewById(R.id.attendanceet);
final EditText input2 = (EditText) DialogView.findViewById(R.id.totalclasseset);

答案 2 :(得分:0)

您必须在调用AlertDialog方法之前创建show()。并且在show()上调用AlertDialog方法,而不是AlertDialogBuilder

// create alert dialog              
AlertDialog alertDialog = alert.create();

请查看this example以供参考。