我试图在对话框中获取选定的日期,但不幸的是我得到了 NullPointerException。
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_add, null));
builder.setTitle("Add new event");
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DatePicker datepicker = (DatePicker) findViewById(R.id.ad_date_picker);
ContentValues values = new ContentValues();
values.put(DbHelper.COLUMN_NAME_DAY, datepicker.getDayOfMonth());
values.put(DbHelper.COLUMN_NAME_MONTH, datepicker.getMonth());
values.put(DbHelper.COLUMN_NAME_YEAR, datepicker.getYear());
DbHelper dbHelper = new DbHelper(getApplicationContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.insert(DbHelper.TABLE_NAME, null, values);
}
});
builder.show();
堆栈跟踪:
03-02 17:34:31.890: E/AndroidRuntime(1619): FATAL EXCEPTION: main
03-02 17:34:31.890: E/AndroidRuntime(1619): java.lang.NullPointerException
03-02 17:34:31.890: E/AndroidRuntime(1619): at com.dennis.countdown.MainActivity$1$1.onClick(MainActivity.java:38)
03-02 17:34:31.890: E/AndroidRuntime(1619): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
03-02 17:34:31.890: E/AndroidRuntime(1619): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 17:34:31.890: E/AndroidRuntime(1619): at android.os.Looper.loop(Looper.java:137)
03-02 17:34:31.890: E/AndroidRuntime(1619): at android.app.ActivityThread.main(ActivityThread.java:5193)
03-02 17:34:31.890: E/AndroidRuntime(1619): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 17:34:31.890: E/AndroidRuntime(1619): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 17:34:31.890: E/AndroidRuntime(1619): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-02 17:34:31.890: E/AndroidRuntime(1619): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-02 17:34:31.890: E/AndroidRuntime(1619): at dalvik.system.NativeStart.main(Native Method)
DatePicker 在 XML 文件中,因为我需要一个带有标题字段和日期选择器的自定义对话框。我没有找到任何其他方法来做到这一点。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name" />
<EditText
android:id="@+id/title"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Title" />
<DatePicker
android:id="@+id/ad_date_picker"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dp"/>
</LinearLayout>