在[dateView.setText(tempDate);]获取NullPointerException ...请提示?基本上我需要从日历中获取更改日期,然后使用这个新日期更新TextView。感谢。
CalendarView calendar;
String tempDate;
TextView dateView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dateView = (TextView)findViewById(R.id.TextDate);
setContentView(R.layout.calendar);
initializeCalendar();
}
public void initializeCalendar() {
calendar = (CalendarView) findViewById(R.id.calendar);
calendar.setShowWeekNumber(false);
calendar.setFirstDayOfWeek(2);
calendar.setSelectedWeekBackgroundColor(getResources().getColor(R.color.lightblue));
calendar.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent));
calendar.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent));
calendar.setSelectedDateVerticalBar(R.color.blue);
calendar.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
tempDate = (day+"/"+month+"/"+year);
dateView.setText(tempDate);
}
});
}
}
错误报告:
java.lang.NullPointerException
at training.mark.trainingii.Calendar$1.onSelectedDayChange(Calendar.java:40)
at android.widget.CalendarView$1.onChanged(CalendarView.java:1012)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
at android.widget.CalendarView$WeeksAdapter.setSelectedDay(CalendarView.java:1375)
at android.widget.CalendarView$WeeksAdapter.onDateTapped(CalendarView.java:1460)
at android.widget.CalendarView$WeeksAdapter.onTouch(CalendarView.java:1448)
at android.view.View.dispatchTouchEvent(View.java:7708)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2329)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1568)
at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2277)
at android.view.View.dispatchPointerEvent(View.java:7893)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5081)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native
答案 0 :(得分:9)
更改订单
setContentView(R.layout.calendar);
dateView = (TextView)findViewById(R.id.TextDate);
您应首先setContentView(R.layout.calendar);
,然后初始化Views
。