简体   繁体   中英

Android: Alert Dialog Box crashing

I have an Alert Dialog Box that pops up on the click of a ListView item. The Alert Dialog has a custom layout containing two EditTexts and a TextView. However on calling EditText.getText() on the click of the OK button on the dialog, the application crashes with java.lang.NullPointerException. Please help me in debugging it.

The listview onClickListener code :

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

The dialog.xml code :

<?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>

You should use the dialog object to intialize input1 an input 2.

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

You can findViewById of the current view hierarchy set to the activity. In your case you inflate a dialog and you current view is the dialog on listview item click. So you should use the dialog object to initialize the views.

You can remove the final modiifier for the below

 AlertDialog.Builder alert = new AlertDialog.Builder(List_of_Courses.this);   
final EditText input1 = (EditText) findViewById(R.id.attendanceet);
final EditText input2 = (EditText) findViewById(R.id.totalclasseset);

should be

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

You have to create an AlertDialog before calling show() method. And call show() method on created AlertDialog not on AlertDialogBuilder .

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

Look at this example for reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM