问题
I've released an app which uses the Android Compatibility Library for Google Maps (https://github.com/petedoyle/android-support-v4-googlemaps) and I've received some odd "Unable to start activity" crash reports.
I've pasted a couple of sample stack traces below. Anyone else experienced this using the Android Compatibility Library for Google Maps or even using the standard Android Compatibility Library (http://developer.android.com/sdk/compatibility-library.html)? Anyone know how I might be able to overcome this error?
----- Example stack trace 1 -----
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myapp/com.mycompany.myapp.activities.GameActivity}: java.lang.NullPointerException
...
caused by java.lang.NullPointerException
at android.app.ContextImpl.openFileOutput(ContextImpl.java:430)
at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
at com.google.common.io.android.AndroidPersistentStore.writeBlockX(Unknown Source)
at com.google.common.io.android.AndroidPersistentStore.writeBlock(Unknown Source)
at com.google.common.io.PreferenceStore.ensurePreferencesLoaded(Unknown Source)
at com.google.common.io.PreferenceStore.readPreference(Unknown Source)
at com.google.common.io.BasePersistentStore.readPreference(Unknown Source)
at com.google.common.StaticUtil.readPreferenceAsDataInput(Unknown Source)
at com.google.googlenav.datarequest.DataRequestDispatcher.loadOrRequestCookie(Unknown Source)
at com.google.googlenav.datarequest.DataRequestDispatcher.<init>(Unknown Source)
at com.google.googlenav.datarequest.DataRequestDispatcher.createInstance(Unknown Source)
at com.google.android.maps.MapActivity.createMap(MapActivity.java:509)
at com.google.android.maps.MapActivity.onCreate(MapActivity.java:409)
at android.support.v4.app.FragmentActivity.onCreate(Unknown Source)
at com.mycompany.myapp.activities.GameActivity.onCreate(Unknown Source)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
----- Example stack trace 2 -----
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myapp/com.mycompany.myapp.activities.GameActivity}: java.lang.NullPointerException
...
Caused by: java.lang.NullPointerException
at android.app.ContextImpl.openFileOutput(ContextImpl.java:423)
at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
at android_maps_conflict_avoidance.com.google.common.io.android.AndroidPersistentStore.writeBlockX(AndroidPersistentStore.java:140)
at android_maps_conflict_avoidance.com.google.common.io.android.AndroidPersistentStore.writeBlock(AndroidPersistentStore.java:169)
at android_maps_conflict_avoidance.com.google.common.io.PreferenceStore.ensurePreferencesLoaded(PreferenceStore.java:178)
at android_maps_conflict_avoidance.com.google.common.io.PreferenceStore.readPreference(PreferenceStore.java:86)
at android_maps_conflict_avoidance.com.google.common.io.BasePersistentStore.readPreference(BasePersistentStore.java:41)
at android_maps_conflict_avoidance.com.google.common.StaticUtil.readPreferenceAsDataInput(StaticUtil.java:271)
at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.loadOrRequestCookie(DataRequestDispatcher.java:493)
at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.<init>(DataRequestDispatcher.java:390)
at android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher.createInstance(DataRequestDispatcher.java:341)
at com.google.android.maps.MapActivity.createMap(MapActivity.java:548)
at com.google.android.maps.MapActivity.onCreate(MapActivity.java:422)
at android.support.v4.app.FragmentActivity.onCreate(Unknown Source)
at com.mycompany.myapp.activities.GameActivity.onCreate(Unknown Source)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1709)
回答1:
I have the same bug and it seems that this error has no link with the usage of android-support-v4-googlemaps.
Take a look at this post :
MapView / MapActivity crash on some devices
回答2:
I had this error when I used the compatibility library. What I had was the project which targeted Android API 15, but I wanted the minimum version to be Android API 8, so I needed a few things from the compatibility library. I have attached the library, but forgot to use imports
from it! When project was built, it was perfectly fine, but when I runned it on my phone which had Android 2.3.3 on it, it couldn't find some classes, because Android 2.3.3 just didn't have them!
So, I suggest you to delete all imports
, press Ctrl+Shift+O
and whenever there will be a choice to use a normal
Android build-in class or one from the compatibility library, always choose the second one.
Tell me if it helped!
来源:https://stackoverflow.com/questions/11055037/unable-to-start-activity-caused-by-nullpointerexception-at-contextimpl-openfileo