2012-12-06 76 views
0

我需要一點幫助。我已經構建了一個自定義警報對話框,其中包含兩個日期選擇器(從 - 到)。我正在從xml文件中爲警報擴展布局,並且當我試圖獲取日期值時,我的程序會兌現。以下是代碼:自定義從AlertDialog

  View fromToLayout = (View)getLayoutInflater().inflate(R.layout.from_to_layout, null); 
     final DatePicker fromPicker = (DatePicker) findViewById(R.id.dpFrom); 
     final DatePicker toPicker = (DatePicker) findViewById(R.id.dpTo); 

     AlertDialog alert = new AlertDialog.Builder(context).create(); 

      alert.setMessage("Insert Interval:");     
      alert.setView(fromToLayout,0,0,0,20); 
      alert.setButton(DialogInterface.BUTTON_POSITIVE,"Ok", new DialogInterface.OnClickListener() 
      { 

       @SuppressWarnings("deprecation") 
       public void onClick(DialogInterface dialog, int which) 
        { 
        // TODO Auto-generated method stub 

         Date dateFrom = new Date(fromPicker.getYear()-1990, fromPicker.getMonth(),fromPicker.getDayOfMonth()); 
         Date dateTo = new Date(toPicker.getYear() - 1990,toPicker.getMonth(),toPicker.getDayOfMonth()); 

         SimpleDateFormat isoFormat = new SimpleDateFormat("yyyyMMdd",Locale.US); 
         SimpleDateFormat dispalyFormat = new SimpleDateFormat("dd-MM-yyyy",Locale.US); 

         String fromDisplay = dispalyFormat.format(dateFrom); 
         String toDisplay = dispalyFormat.format(dateTo); 
         String fromValue = isoFormat.format(dateFrom); 
         String toValue = isoFormat.format(dateTo); 

         CashInInfoActivity.this.setTitle("Cash In " + fromDisplay + " - " + toDisplay);      
         DownloadCashInAsyncTask task = new DownloadCashInAsyncTask(context); 
         task.execute(fromValue,toValue); 

         return; 
        } 
      }); 

      alert.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel", new DialogInterface.OnClickListener() 
      { 

      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       return; 
       } 
      }); 

      alert.show(); 

我在行得到NullPointerException異常:Date dateFrom = new Date(fromPicker.getYear()-1990, fromPicker.getMonth(),fromPicker.getDayOfMonth());

登錄:

12-06 11:01:48.919: E/AndroidRuntime(24121): FATAL EXCEPTION: main 
12-06 11:01:48.919: E/AndroidRuntime(24121): java.lang.NullPointerException 
12-06 11:01:48.919: E/AndroidRuntime(24121): at com.example.denandroidapp.CashInInfoActivity$1.onClick(CashInInfoActivity.java:98) 
12-06 11:01:48.919: E/AndroidRuntime(24121): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159) 
12-06 11:01:48.919: E/AndroidRuntime(24121): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-06 11:01:48.919: E/AndroidRuntime(24121): at android.os.Looper.loop(Looper.java:123) 
12-06 11:01:48.919: E/AndroidRuntime(24121): at android.app.ActivityThread.main(ActivityThread.java:3687) 
12-06 11:01:48.919: E/AndroidRuntime(24121): at java.lang.reflect.Method.invokeNative(Native Method) 
12-06 11:01:48.919: E/AndroidRuntime(24121): at java.lang.reflect.Method.invoke(Method.java:507) 
12-06 11:01:48.919: E/AndroidRuntime(24121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
12-06 11:01:48.919: E/AndroidRuntime(24121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
12-06 11:01:48.919: E/AndroidRuntime(24121): at dalvik.system.NativeStart.main(Native Method) 

你知道什麼我'做錯了THX

+0

發佈錯誤logcat。 –

+0

檢查'onPicker'和'toPicker'在'onClick'方法中是否爲空。他們是 – vasart

+0

,但我不知道爲什麼。 –

回答

1

你得到空指針異常的 「fromPicker」,相同的異常也將適用於 「toPicker」 也。

原因的例外是:

  • 要動態充氣佈局和你想要的DatePicker的參考。
  • 記住,當你不斷膨脹動態任何佈局,並希望獲取它的控制,那麼你必須調用findViewById與視圖的對象,像下面

View fromToLayout = (View)getLayoutInflater().inflate(R.layout.from_to_layout, null); 
final DatePicker fromPicker = (DatePicker) fromToLayout.findViewById(R.id.dpFrom); 
final DatePicker toPicker = (DatePicker) fromToLayout.findViewById(R.id.dpTo); 
1

嘗試?變化:

final DatePicker fromPicker = (DatePicker) findViewById(R.id.dpFrom); 
final DatePicker toPicker = (DatePicker) findViewById(R.id.dpTo); 

到:

final DatePicker fromPicker = (DatePicker) fromToLayout.findViewById(R.id.dpFrom); 
final DatePicker toPicker = (DatePicker) fromToLayout.findViewById(R.id.dpTo);