getDayOfMonth抛出NullPointerException

时间:2013-03-02 15:48:14

标签: android nullpointerexception

我正在尝试在对话框中获取所选日期,但不幸的是我得到了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>

2 个答案:

答案 0 :(得分:1)

Your DatePicker is null,当您尝试访问datepicker.getDayOfMonth()时,它会抛出NPE 您需要在onCreate

中初始化DatePicker
  DatePicker datepicker = (DatePicker) findViewById(R.id.ad_date_picker);

如果你的日期选择器在inflatted视图中,而不是从inflatted视图中获取它

 DatePicker datepicker = (DatePicker) inflattedview.findViewById(R.id.ad_date_picker);

答案 1 :(得分:0)

我的问题在Pragnani的帮助下得到了解决。

而不是:

LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_add, null));
DatePicker datepicker = (DatePicker) findViewById(R.id.ad_date_picker);

我用过:

View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_add, null);
builder.setView(view);
DatePicker datepicker = (DatePicker) view.findViewById(R.id.ad_date_picker);