2013-03-02 67 views
0

我想在對話框中獲取選定的日期,但不幸的是我得到了NullPointerException。getDayOfMonth引發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> 
+0

一個'R','R.id',或'datepicker'必須爲空。那麼哪條線是38? – 2013-03-02 15:51:50

+0

你是什麼意思? – Dennis 2013-03-02 15:53:20

+0

你有沒有在XML中設置DatePicker的值? – IronBlossom 2013-03-02 15:54:57

回答

1

Your DatePicker is null,當您嘗試訪問datepicker.getDayOfMonth(),它拋出NPE 需要初始化你的DatePicker中的onCreate

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

如果你的日期選取器是否有比得到inflatted視圖它從inflatted視圖

DatePicker datepicker = (DatePicker) inflattedview.findViewById(R.id.ad_date_picker); 
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); 
相關問題