简体   繁体   中英

impossible nullpointer exception at calling member function from onWindowFocusChanged

please help to understand a strange nullpointer exception, which seems to happen only on specific devices (currently reported only from different tablets, eg: Nexus-7, Asus, ...).

The nullpointer exception occurs while calling the current activity's member function from an other member function:

10-22 13:50:09.190 E/AndroidRuntime(4445): FATAL EXCEPTION: main
10-22 13:50:09.190 E/AndroidRuntime(4445): java.lang.NullPointerException
10-22 13:50:09.190 E/AndroidRuntime(4445): at com.plasmobit.CosmicPatrol.ActLogBook.onWindowFocusChanged(SourceFile:194)
10-22 13:50:09.190 E/AndroidRuntime(4445): at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2366)
10-22 13:50:09.190 E/AndroidRuntime(4445): at android.view.View.dispatchWindowFocusChanged(View.java:5740)
10-22 13:50:09.190 E/AndroidRuntime(4445): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:851)
10-22 13:50:09.190 E/AndroidRuntime(4445): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2557)
10-22 13:50:09.190 E/AndroidRuntime(4445): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 13:50:09.190 E/AndroidRuntime(4445): at android.os.Looper.loop(Looper.java:137)
10-22 13:50:09.190 E/AndroidRuntime(4445): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-22 13:50:09.190 E/AndroidRuntime(4445): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 13:50:09.190 E/AndroidRuntime(4445): at java.lang.reflect.Method.invoke(Method.java:511)
10-22 13:50:09.190 E/AndroidRuntime(4445): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-22 13:50:09.190 E/AndroidRuntime(4445): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-22 13:50:09.190 E/AndroidRuntime(4445): at dalvik.system.NativeStart.main(Native Method)

The code at the line 194 is part of the overridden onWindowFocusChange activity interface. It is just a call of a parameterless member function:

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    ...
    doSomeStuff();  // Line: 194
    ...
}

private void doSomeStuff()
{
    ....
}

Because we are inside of the onWindowFocusChange function, the this pointer of the ActLogBook activity instance shall be valid, so how is here a nullpointer exceptoin possible?

Do not know if you ever figured this out. I have just encountered a similar issue and since this was the only related Google search thought I would share my findings in case its ever helpful to someone.

I had a reproducible NPE thrown by an argumentless method call to another method on the same instance. Eventually I thought to try building my apk without Proguard. Suddenly the stack trace has another line visible at the top from inside the method that had appeared to be throwing the NPE. It then made a lot more sense.

So try again but leave off Proguard during your debugging. I guess there must be some bug somewhere that makes Proguard sometimes omit lines of the top of stacktraces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM