0

我需要一点帮助。我已经构建了一个自定义警报对话框,其中包含两个日期选择器(从 - 到)。我正在从 xml 文件中为警报的布局充气,当我试图获取我的程序兑现的日期值时。这里是代码:

         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

4

2 回答 2

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);
于 2012-12-06T09:43:43.227 回答
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);
于 2012-12-06T09:44:36.030 回答