Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException in ActivityThread.handleBindApplication

Tags:

android

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?

like image 216
Emil Davtyan Avatar asked Apr 10 '26 09:04

Emil Davtyan


2 Answers

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");
        }
    }
like image 199
Emil Davtyan Avatar answered Apr 13 '26 00:04

Emil Davtyan


I generally find I get these kind of exceptions when a service, activity or whatever has problems initiating itself. Typically I have done something bad (TM) in my variable initiations in the class or I have done something else bad to the layout or views I am instantiating. I would start by looking at what is being started up at the time this happens.

Another easy way to trigger this kind of exception is to call and activity (or service etc) without having declared it as such in the manifest. Or try to do something that requires a permission and forget to declare that (although it doesn't look like that error in this case).

like image 33
Neil Townsend Avatar answered Apr 12 '26 23:04

Neil Townsend