[英]NullPointer Exception when calling a method from onResume
我在longPress上設置了selectDate,但為什么在HighlightCalander函數中卻說我的selectDate為null? 有任何想法嗎。
在onCreate(主要活動)中調用此eventHandler:
calendarview.setEventHandler(new CalendarView.EventHandler()
{
@Override
public void onDayLongPress(Date date)
{
DateFormat df = SimpleDateFormat.getDateInstance();
selectDate = date;
System.out.println(selectDate);
intent1 = new Intent(getApplicationContext(), MakeAppointmentsActivity.class);
intent1.putExtra("DATE",df.format(date));
startActivity(intent1);
}
此函數在onResume(主要活動)中調用:-
public void HighlightCalendar()
{
intent2 = getIntent();
// get my boolean from save button
boolean savedDate = intent2.getBooleanExtra("savedDate", false);
// if i pressed my saved button
if(savedDate) {
Toast.makeText(this,"true",Toast.LENGTH_SHORT).show();
try {
DateFormat df = SimpleDateFormat.getDateInstance();
SimpleDateFormat curFormater = new SimpleDateFormat("MMM yyyy");
String dateString = df.format(selectDate);
Date dateObj = curFormater.parse(dateString);
events.add(dateObj);
calendarview.updateCalendar(events);
}catch (ParseException e){
e.printStackTrace();
}
}
else
{
Toast.makeText(this,"false",Toast.LENGTH_SHORT).show();
}
}
您在onDayLongPress
為selectDate
分配了一個值,但在onResume
中使用了它。 每次創建Activity
,都會調用onCreate()
和onResume()
。 您將收到NUllpointerException
因為在onResume()
(在onDayLongPress
之前onDayLongPress
)中, selectDate
未selectDate
,因此為null
。 您可以使用兩種方法之一來selectDate
以避免異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.