2016-07-09 8 views
0

私はlongPressでselectDateを設定していますが、なぜ私のselectDateがHighlightCalander関数でnullであると言われますか?何か案は。onResumeからメソッドを呼び出すときのNullPointer例外

このEventHandlerはのonCreate(主な活動)に呼び出されます - この関数は、onResume(主な活動)に

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); 
      } 

と呼ばれている: - あなたは割り当てている

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(); 
     } 
    } 
+0

'savedDate'の代わりに' DATE'を渡すと 'df.format(date)'のために 'bool'の代わりにIntentから文字列を取得しますreturn String –

+0

[NullPointerExceptionとは何ですか? ](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –

答えて

1

値はselectDateonDayLongPressで表示されますが、onResumeで使用しています。毎回ActivityonCreate()onResume()を作成します。 NUllpointerExceptionが届いています。onResume()onDayLongPressより前に)selectDateが有効になっていないため、nullです。例外を回避するには、2つの方法のいずれかでselectDateを初期化することができます。

関連する問題