我有一个在单击 ListView 项目时弹出的警报对话框。警报对话框有一个自定义布局,其中包含两个 EditText 和一个 TextView。但是,在单击对话框上的 OK 按钮时调用 EditText.getText(),应用程序会因 java.lang.NullPointerException 而崩溃。请帮我调试它。
列表视图 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>