java.lang.NullPointerException: Attempt to invoke

2019-01-27 01:29发布

问题:

I have an exception on Crashlytics that doesn't show any of my code on the stack trace, how can I figure out what is causing it? Seems to be happening exclusively on Android 5.0 and 5.0.1. I am using the support library 23.4.

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Editor$SelectionModifierCursorController.show()' on a null object reference
       at android.widget.Editor$SelectionActionModeCallback.onCreateActionMode(Editor.java:3023)
       at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onCreateActionMode(PhoneWindow.java:3163)
       at android.support.v7.view.SupportActionModeWrapper$CallbackWrapper.onCreateActionMode(SupportActionModeWrapper.java:155)
       at android.support.v7.app.AppCompatDelegateImplV7$ActionModeCallbackWrapperV7.onCreateActionMode(AppCompatDelegateImplV7.java:1742)
       at android.support.v7.app.AppCompatDelegateImplV7.startSupportActionModeFromWindow(AppCompatDelegateImplV7.java:802)
       at android.support.v7.app.AppCompatDelegateImplV7.startSupportActionMode(AppCompatDelegateImplV7.java:696)
       at android.support.v7.app.AppCompatDelegateImplV14$AppCompatWindowCallbackV14.startAsSupportActionMode(AppCompatDelegateImplV14.java:187)
       at android.support.v7.app.AppCompatDelegateImplV14$AppCompatWindowCallbackV14.onWindowStartingActionMode(AppCompatDelegateImplV14.java:171)
       at android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode(WindowCallbackWrapper.java:145)
       at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode(PhoneWindow.java:2644)
       at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionModeForChild(PhoneWindow.java:2631)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
       at android.view.View.startActionMode(View.java:4857)
       at android.widget.Editor.startSelectionActionMode(Editor.java:1626)
       at android.widget.Editor.performLongClick(Editor.java:914)
       at android.widget.TextView.performLongClick(TextView.java:8900)
       at android.view.View$CheckForLongPress.run(View.java:19735)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5253)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

EDIT1: A user who reported the crash figured out how to reproduce it. It happens when he uses my AppCompatAutoCompleteTextView to select all the text and copy it to the clipboard. It only happens on Android 5.0 and 5.0.1

EDIT2: I think it is happening on more than just Android 5 but the stacktrace looks different enough that Crashlytics doesn't group it. Based on some posts I found I think it has to do with the Contextual Actionbar height or how it calculates it but I haven't found a clear answer yet.

回答1:

I have a AppCompatAutoCompleteTextView that changes size when it is on focus. Turns out users were long pressing on it while it was on the collapsed size and when the onFocusChange event fired the whole thing would change and it would crash.



回答2:

You can reproduce that by calling setText("test"); inside onFocusChange and long pressing that EditText on Samsung s8 (I am not sure about other devices).

As a solution you may try to get rid of setting text or other manipulations which causes text rearrangement in onFocusChange or delaying it with handler.