尝试从空对象引用上的字段“android.view.View androidx.recyclerview.widget.RecyclerView$b0.a”读取

Posted

技术标签:

【中文标题】尝试从空对象引用上的字段“android.view.View androidx.recyclerview.widget.RecyclerView$b0.a”读取【英文标题】:Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$b0.a' on a null object reference 【发布时间】:2022-01-22 08:46:45 【问题描述】:

我正在开发一个执行 BLE 扫描和更新回收站视图的应用程序。该应用程序在调试模式下在 android studio 中运行良好。但是当我在 Play Store 上发布生产时,它会崩溃并出现以下错误(来自 crashlytics)..

Fatal Exception : java.lang.NullPointerException: Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$b0.a' on a null object reference
       at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:59)
       at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:59)
       at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3)
       at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:3)
       at androidx.recyclerview.widget.RecyclerView$LayoutManager.addView(RecyclerView.java)
       at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:2)
       at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:17)
       at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:2)
       at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:7)
       at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:2)
       at android.view.View.layout(View.java:22900)
       at android.view.ViewGroup.layout(ViewGroup.java:6389)
       at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:66)
       at android.view.View.layout(View.java:22900)
       at android.view.ViewGroup.layout(ViewGroup.java:6389)
       at androidx.coordinatorlayout.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:15)
       at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:4)
       at android.view.View.layout(View.java:22900)
       at android.view.ViewGroup.layout(ViewGroup.java:6389)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
       at android.view.View.layout(View.java:22900)
       at android.view.ViewGroup.layout(ViewGroup.java:6389)
       at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:49)
       at android.view.View.layout(View.java:22900)
       at android.view.ViewGroup.layout(ViewGroup.java:6389)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
       at android.view.View.layout(View.java:22900)
       at android.view.ViewGroup.layout(ViewGroup.java:6389)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
       at android.view.View.layout(View.java:22900)
       at android.view.ViewGroup.layout(ViewGroup.java:6389)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
       at com.android.internal.policy.DecorView.onLayout(DecorView.java:831)
       at android.view.View.layout(View.java:22900)
       at android.view.ViewGroup.layout(ViewGroup.java:6389)
       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3619)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3087)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2060)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8421)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
       at android.view.Choreographer.doCallbacks(Choreographer.java:796)
       at android.view.Choreographer.doFrame(Choreographer.java:731)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:250)
       at android.app.ActivityThread.main(ActivityThread.java:7886)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:970)

我正在尝试this 解决方案但遇到同样的问题。

这是视图持有者通用类:

  @Override
    public V onCreateViewHolder(ViewGroup parent,
                                int viewType) . ///
        // create a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(viewResId, parent, false);

        Class[] args = new Class[1];
        args[0] = View.class;

        try 
            return holderClass.getDeclaredConstructor(args).newInstance(v);
         catch (Exception e) 
            if (e != null && e.getMessage() != null) 
                AppLogger.e(RecyclerListAdapter.class.getSimpleName(), e.getMessage());
                e.printStackTrace();
            
            return null;
        
    

【问题讨论】:

【参考方案1】:

问题是在未指定 ProGuard 规则的情况下将 .aab 文件上传到游戏控制台。

您可以在此处阅读更多信息https://developer.android.com/studio/build/shrink-code

【讨论】:

以上是关于尝试从空对象引用上的字段“android.view.View androidx.recyclerview.widget.RecyclerView$b0.a”读取的主要内容,如果未能解决你的问题,请参考以下文章

尝试从空对象引用上的字段“android.view.View androidx.recyclerview.widget.RecyclerView$b0.a”读取

空对象引用上的 void android.view.View.dispatchWindowVisibilityChanged(int)'

尝试在空对象引用上调用虚拟方法 'android.view.SurfaceHolder android.view.SurfaceView.getHolder()'

尝试在空对象引用上调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()'

Android Studio:尝试在空对象引用上调用虚拟方法“void android.view.View.setOnClickListener”

尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”