我正在尝试以编程方式为 CalendarView 小部件设置最小日期,如下所示:
setContentView(R.layout.dialog_date_select);
CalendarView calendar = (CalendarView) findViewById(R.id.calendarView1);
calendar.setMinDate(System.currentTimeMillis() - 1000);
但由于某种原因,我收到以下错误
11-05 12:50:14.054: E/AndroidRuntime(798): FATAL EXCEPTION: main
11-05 12:50:14.054: E/AndroidRuntime(798): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.patientsnow/com.patientsnow.activity.DateSelectActivity}:
java.lang.NullPointerException
11-05 12:50:14.054: E/AndroidRuntime(798): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-05 12:50:14.054: E/AndroidRuntime(798): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-05 12:50:14.054: E/AndroidRuntime(798): at
android.app.ActivityThread.access$600(ActivityThread.java:141)
11-05 12:50:14.054: E/AndroidRuntime(798): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-05 12:50:14.054: E/AndroidRuntime(798): at
android.os.Handler.dispatchMessage(Handler.java:99)
11-05 12:50:14.054: E/AndroidRuntime(798): at android.os.Looper.loop(Looper.java:137)
11-05 12:50:14.054: E/AndroidRuntime(798): at
android.app.ActivityThread.main(ActivityThread.java:5103)
11-05 12:50:14.054: E/AndroidRuntime(798): at
java.lang.reflect.Method.invokeNative(Native Method)
11-05 12:50:14.054: E/AndroidRuntime(798): at
java.lang.reflect.Method.invoke(Method.java:525)
11-05 12:50:14.054: E/AndroidRuntime(798): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-05 12:50:14.054: E/AndroidRuntime(798): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-05 12:50:14.054: E/AndroidRuntime(798): at dalvik.system.NativeStart.main(Native
Method)
11-05 12:50:14.054: E/AndroidRuntime(798): Caused by: java.lang.NullPointerException
11-05 12:50:14.054: E/AndroidRuntime(798): at
com.patientsnow.activity.DateSelectActivity.onCreate(DateSelectActivity.java:36)
11-05 12:50:14.054: E/AndroidRuntime(798): at
android.app.Activity.performCreate(Activity.java:5133)
11-05 12:50:14.054: E/AndroidRuntime(798): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-05 12:50:14.054: E/AndroidRuntime(798): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-05 12:50:14.054: E/AndroidRuntime(798): ... 11 more
DateSelectActivity 的第 36 行是我尝试设置MinDate 的地方。为什么此代码未设置最短日期?请注意,如果我尝试在 XML 中设置最小日期和最大日期,那么它可以工作。