Why is NPE thrown when Activity completes?

I have a mistake that made me go crazy for several days. Unfortunately, I canโ€™t show you the code for two reasons, my boss will not appreciate it, and the code base is too large for sharing. An error occurs when an Activity completes. The weather is because I call the finish line () or because os destroys it.

The question is what (or can) call execPendingActions () in FragmentManagerImpl to throw the NPE on line 1196.

Here is the stack:

FragmentManagerImpl.execPendingActions() line: 1196 FragmentManagerImpl$1.run() line: 375 Handler.handleCallback(Message) line: 587 Handler.dispatchMessage(Message) line: 92 Looper.loop() line: 126 ActivityThread.main(String[]) line: 3997 Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] Method.invoke(Object, Object...) line: 491 ZygoteInit$MethodAndArgsCaller.run() line: 841 ZygoteInit.main(String[]) line: 599 NativeStart.main(String[]) line: not available [native method] 
+4
source share
4 answers

You can try using the compat library and debug this.

Alternatively, try and reproduce in a reduced sample application.

+1
source

Speaking of completing an activity: are you trying to execute finish() activity from a view? Are MyClass.this.finish() sent MyClass.this.finish() ?

It seems to me that the OS forces it to shut down due to NPE. Something is wrong with the fragments. There is something Null in them. Could you at least insert a line where it crashes?

+2
source

You may encounter an error in the fragment manager when it does not clear the list of pending transactions before they are destroyed. This can happen if you make a transaction after saving a state using FragmentTransaction.commitAllowingStateLoss (). You can work with this code in your activity:

 @Override void onDestroy() { getFragmentManager().executePendingTransactions(); super.onDestroy(); } 
+2
source

The NullPointerException is thrown due to the fact that the activity handler is not configured from the FragmentManager, so the โ€œsolutionโ€ that will prevent the crash will be as follows:

 public void onDestroy(){ super.onDestroy(); try { Field mActivityField = getFragmentManager().getClass().getDeclaredField("mActivity"); mActivityField.setAccessible(true); mActivityField.set(getFragmentManager(), this); Field mPendingActionsField = getFragmentManager().getClass().getDeclaredField("mPendingActions"); mPendingActionsField.setAccessible(true); mPendingActionsField.set(getFragmentManager(), null); Field f = Activity.class.getDeclaredField("mHandler"); f.setAccessible(true); Handler handler = (Handler) f.get(this); handler.close(); } catch (Throwable e) { } } 
0
source

All Articles