簡體   English   中英

從onResume調用方法時發生NullPointer異常

[英]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();
        }
    }

您在onDayLongPressselectDate分配了一個值,但在onResume中使用了它。 每次創建Activity ,都會調用onCreate()onResume() 您將收到NUllpointerException因為在onResume() (在onDayLongPress之前onDayLongPress )中, selectDateselectDate ,因此為null 您可以使用兩種方法之一來selectDate以避免異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM