James andresakis
James andresakis

Reputation: 5415

Android - Force close when no sd card is present

So I have an application which stores files in directories that I create when the user gets within a certain distance of a geographic location. Everything works fine except for when certain devices that usually use an sd card have it removed and then the app crashes when a user logs in. I tried on a droid x and I was able to replicate the crash. When I have the sd card in it works fine but when the sd card is removed I get a nullpointer exception and force close.

Heres the log dump from the market:

java.lang.RuntimeException: Unable to resume activity       {graffit.cores/graffit.cores.GraffView}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2208)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2228)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1721)
at android.app.ActivityThread.access$1500(ActivityThread.java:124)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at graffit.cores.GraffView.onResume(GraffView.java:1201)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
at android.app.Activity.performResume(Activity.java:3882)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2191)
... 12 more
java.lang.NullPointerException
at graffit.cores.GraffView.onResume(GraffView.java:1201)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
at android.app.Activity.performResume(Activity.java:3882)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2191)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2228)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1721)
at android.app.ActivityThread.access$1500(ActivityThread.java:124)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

Being that the logdump says something about failing on instrumentation Im guessing that I should catch that or look for if an sd card is available.

Im sure I can find out how to look for an sd card but how can I catch this crash and use internal storage in place of a missing sd card.

Any help would be much appreciated.

Upvotes: 0

Views: 740

Answers (1)

Wizetux
Wizetux

Reputation: 756

Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available. The media might not always be available, such as in your case. The Android Docs give an example on how to deal with this: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

The best thing to do is probably do this check in a function that returns a Boolean value on whether you can get to the external storage and then check it each time you need to get data from the card.

if (sdCardAvalableForRead())
{
   // get file.
}
else
{
   // show warning or message to user.
}

Upvotes: 1

Related Questions