Android アプリケーションから、奇妙なエラー メッセージが表示されました: NullPointerException (最後の行のコード)。
public static Integer getDefaultCalendarId(Context context) {
String calendarInSettings = PrefsHelper.getDefaultCalendar(context);
Calendar calendar = PrefsHelper.getCalendarFromPrefKey(context, calendarInSettings);
if (calendar == null || !calendar.canEventEdit()) {
return calculateDefaultCalendar(context);
}
boolean isChecked = false;
for (Calendar checkedCalendar : PrefsHelper.getCheckedCalendars(context)) {
if (checkedCalendar.getId() == calendar.getId()) {
isChecked = true;
break;
}
}
(1114 line) return isChecked ? calendar.getId() : calculateDefaultCalendar(context); // NullPointerException
}
Samsung GT-N7000 で例外が発生しました。
これはどのように起こりますか?
アップデート:
calculateDefaultCalendar は null を返すことができますが、getDefaultCalendarId の戻り値は整数 (null 可能) です。
スタックトレース:
java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.trial/xxx.view.EventEditActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:986)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at xxx.EventHelper.java.lang.Integer getDefaultCalendarId(android.content.Context)(SourceFile:1114)
at xxx.view.EventEditActivity.void onCreate(android.os.Bundle)(SourceFile:306)
at android.app.Activity.performCreate(Activity.java:4470)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
... 11 more
java.lang.NullPointerException
at xxx.EventHelper.java.lang.Integer getDefaultCalendarId(android.content.Context)(SourceFile:1114)
at xxx.view.EventEditActivity.void onCreate(android.os.Bundle)(SourceFile:306)
at android.app.Activity.performCreate(Activity.java:4470)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:986)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)
at dalvik.system.NativeStart.main(Native Method)
更新 2
calculateDefaultCalendar:
private static Integer calculateDefaultCalendar(final Context context) {
String account = PrefsHelper.getAccountName(context);
if (account != null) {
for (Calendar calendar : PrefsHelper.getCheckedCalendars(context)) {
if (calendar.getOwnerAccount().equals(account) && calendar.canEventEdit()) {
return calendar.getId();
}
}
}
for (Calendar calendar : PrefsHelper.getCheckedCalendars(context)) {
if (calendar.canEventEdit()) {
return calendar.getId();
}
}
return null;
}
アップデート 3
エラーは定期的に繰り返されます:
製品 / アンドロイド版
LT26i_1257-3921 / 4.0.4
MK16i_1249-8137 / 4.0.4
SCH-I500 / 2.3.5
SCH-I500 / 4.0.4