Skip to content Skip to sidebar Skip to footer

Nullpointerexception (etc) From Parcel.readexception

Exceptions that look like this are confusing: FATAL EXCEPTION: main java.lang.NullPointerException at android.os.Parcel.readException(Parcel.java:1437) at android.os.Parcel

Solution 1:

What's going on here is that readException() is checking the IPC byte stream for a header that says that an exception occurred; if it finds one, then it throws a new exception of that type, with the same message, but missing the original stack trace. (It only actually knows a few exception types; anything else gets translated into a base RuntimeException.)

So where's the original exception coming from? Well, somewhere down in the guts of the real implementation of YourClass.yourMethod() -- not in any of the parcelable or IPC code. So go there, wrap the whole method in a try/catch, and log whatever you caught.

(Or set a breakpoint there if you've got remote process breakpoints working.)

Solution 2:

I think android SHOULD provide more binder remote exception information

so i modify Parcel.java to wrap more binder remote exception information

publicfinalvoid writeException(Exception e) {
    int code = 0;
    if (e instanceof SecurityException) {
        code = EX_SECURITY;
    } elseif (e instanceof BadParcelableException) {
        code = EX_BAD_PARCELABLE;
    } elseif (e instanceof IllegalArgumentException) {
        code = EX_ILLEGAL_ARGUMENT;
    } elseif (e instanceof NullPointerException) {
        code = EX_NULL_POINTER;
    } elseif (e instanceof IllegalStateException) {
        code = EX_ILLEGAL_STATE;
    }
    writeInt(code);
    StrictMode.clearGatheredViolations();
    if (code == 0) {
        if (e instanceofRuntimeException) {
            throw (RuntimeException) e;
        }
        thrownewRuntimeException(e);
    }
    // I replace writeString(e.getMessage()) with writeString(remoteExceptionToString(e))
    writeString(remoteExceptionToString(e));
}

staticString remoteExceptionToString(Exception e) {
    final StringWriter sw = new StringWriter(1024);
    final PrintWriter pw = new PrintWriter(sw, true);
    pw.println();
    e.printStackTrace(pw);
    return sw.toString().replace("\n", String.format("\n(%5d %5d): ", Process.myPid(), Process.myTid()));
}

SerializeExceptionSecondService defination:

publicclassSerializeExceptionSecondServiceextendsService {
privatestaticfinalStringTAG="SerializeExceptionSecondService";

publicSerializeExceptionSecondService() {
}

@Overridepublic IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.return mServiceBinder;
}

privatefinal ISerializeExceptionSecondService.StubmServiceBinder=newISerializeExceptionSecondService.Stub() {

    @OverridepublicvoidthrowException()throws RemoteException {
        // TODO Auto-generated method stub
        Log.e(TAG, "throwException");
        thrownewIllegalStateException("Cause1", newIllegalStateException("Cause2", newIllegalStateException("Cause3")));
    }

    @OverridepublicvoidnoThrowException()throws RemoteException {
        // TODO Auto-generated method stub

    }
};
}

AndroidManifest.xml fragment:

<serviceandroid:name=".SerializeExceptionSecondService"android:enabled="true"android:exported="true"android:process=":second_remote" ></service>

in this way, the binder exception logcat will look like the below:

enter image description here

Post a Comment for "Nullpointerexception (etc) From Parcel.readexception"