我需要一点帮助。我已经构建了一个自定义警报对话框,其中包含两个日期选择器(从 - 到)。我正在从 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