NullPointerException in ActivityThread.handleBindApplication

android

Solution

As always when this happens I have to dig through the Android source to assess the problem, and I thought I would post my findings here so it would save others time.

This error corresponds to this code in 4.1.1_r1 and 4.1.2_r1:

    final ContextImpl appContext = new ContextImpl();
    appContext.init(data.info, null, this);
    final File cacheDir = appContext.getCacheDir();

    // Provide a usable directory for temporary files
    System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath()); // line 3979

    setupGraphicsSupport(data.info, cacheDir);

This is happening because `appContext.getCacheDir()` returns null in some instances:

@Override
public File getCacheDir() {
    synchronized (mSync) {
        if (mCacheDir == null) {
            mCacheDir = new File(getDataDirFile(), "cache");
        }
        if (!mCacheDir.exists()) {
            if(!mCacheDir.mkdirs()) {
                Log.w(TAG, "Unable to create cache directory");
                return null;
            }
            FileUtils.setPermissions(
                    mCacheDir.getPath(),
                    FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
                    -1, -1);
        }
    }
    return mCacheDir;
}

Related: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/-694j87eXVU

However this seems to be being handled properly in version 4.2.1:

    final ContextImpl appContext = new ContextImpl();
    appContext.init(data.info, null, this);
    if (!Process.isIsolated()) {
        final File cacheDir = appContext.getCacheDir();

        if (cacheDir != null) {
            // Provide a usable directory for temporary files
            System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());

            setupGraphicsSupport(data.info, cacheDir);
        } else {
            Log.e(TAG, "Unable to setupGraphicsSupport due to missing cache directory");
        }
    }

Problem

I have received this stack trace report that does not mention my app at all: ``` java.lang.NullPointerException at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3979) at android.app.ActivityThread.access$1300(ActivityThread.java:130) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) 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:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) ``` Does anybody have any idea how to prevent this exception?

Original source