最近遇到一个偶先的问题,APP在遥控器操作的时候,偶先崩溃。日志如下:
12-22 19:54:53.359 30204 30204 E AndroidRuntime: java.lang.IllegalStateException: super.focusSearch(focused, direction) must not be null
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at com.xxxxx.focusSearch(CustomView.kt:492)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewGroup.focusSearch(ViewGroup.java:871)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewGroup.focusSearch(ViewGroup.java:871)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at androidx.leanback.widget.GridLayoutManager.onInterceptFocusSearch(GridLayoutManager.java:3349)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at androidx.recyclerview.widget.RecyclerView.focusSearch(RecyclerView.java:2748)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.View.focusSearch(View.java:8661)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4391)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4298)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4028)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4189)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2365)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1961)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1952)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2342)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.os.MessageQueue.nativePollOnce(Native Method)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.os.MessageQueue.next(MessageQueue.java:323)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
12-22 19:54:53.359 30204 30204 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6097)
当时的代码类似如下代码:
override fun focusSearch(focused: View?, direction: Int): View {
if (description.hasFocus() && direction == View.FOCUS_DOWN) {
val countTextView = getCountTextView()
if (countTextView != null) {
return countTextView
}
}
return super.focusSearch(focused, direction)
}
为了这个问题想了半天,觉得逻辑也没有什么不对的...直到最后才看到,复写后的方法返回值为Kotlin中的“非空对象”!!导致super.focusSearch(focused,direction)方法返回null的话,触发kotlin的异常报错!
解决方法只需要把返回值「View」改成「View?」就好了!这Android Studio的Kotlin支持真是离了大谱!
|