今天使用 ViewBinding 时遇到一个 Crash:java.lang.NullPointerException: Missing required view with ID
,最终发现是与自定义 View 有关系……
一、背景
最近使用 ViewBinding
时,遇到这么一个报错:
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: me.hjhl.app, PID: 10740
E AndroidRuntime: java.lang.NullPointerException: Missing required view with ID: me.hjhl.app:id/my_gl_surface_view
E AndroidRuntime: at me.hjhl.app.databinding.FragmentGlesDemoBinding.bind(FragmentGlesDemoBinding.java:67)
E AndroidRuntime: at me.hjhl.app.databinding.FragmentGlesDemoBinding.inflate(FragmentGlesDemoBinding.java:49)
E AndroidRuntime: at me.hjhl.app.demo.GLESDemoFragment.onCreateView(GLESDemoFragment.kt:31)
E AndroidRuntime: at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
E AndroidRuntime: at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
E AndroidRuntime: at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
E AndroidRuntime: at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
E AndroidRuntime: at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1764)
E AndroidRuntime: at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701)
E AndroidRuntime: at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2849)
E AndroidRuntime: at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2784)
E AndroidRuntime: at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:262)
E AndroidRuntime: at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:478)
E AndroidRuntime: at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
E AndroidRuntime: at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1433)
E AndroidRuntime: at android.app.Activity.performStart(Activity.java:7923)
E AndroidRuntime: at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3337)
E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:107)
E AndroidRuntime: at android.os.Looper.loop(Looper.java:228)
E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7589)
E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:953)
整体代码逻辑大致是:Activity
创建时跳转到一个 Fragment
,这个 Fragment
对应的 XML 里很简单——FrameLayout
里套了一个自定义 View。错误堆栈提示找不到资源 id my_gl_surface_view
。这让我不禁怀疑是不是自定义 View 导致的。
二、分析过程
2.1 找到 ViewBinding
生成的 UI 类
ViewBinding 的大致原理是在编译时,把开启了 ViewBinding 功能的布局资源 XML 生成对应的 Java 类。例如在这个 case 中,对应的类文件位置为:app/build/generated/data_binding_base_class_source_out/debug/out/me/ljh/app/databinding/FragmentGlesDemoBinding.java
。
2.2 分析关键代码
以下代码片段是从 FragmentGlesDemoBinding
中摘出:
// file: app/build/generated/data_binding_base_class_source_out/debug/out/me/ljh/app/databinding/FragmentGlesDemoBinding.java
public final class FragmentGlesDemoBinding implements ViewBinding {
@NonNull
public static F