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

Posted

技术标签:

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

我正在制作一个应用程序,它从 sql 获取数据并将其放入回收视图中我的数据进入回收视图,它在 logcat 中给了我这个错误。

 java.lang.NullPointerException: Attempt to read from field
       'android.view.View
       androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' on a
       null object reference
               at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7079)
               at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
               at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
               at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
               at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
               at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
               at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
               at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
               at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
               at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
               at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
               at android.view.View.layout(View.java:18010)
               at android.view.ViewGroup.layout(ViewGroup.java:5817)
               at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
               at android.view.View.layout(View.java:18010)
               at android.view.ViewGroup.layout(ViewGroup.java:5817)
               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
               at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
               at android.view.View.layout(View.java:18010)
               at android.view.ViewGroup.layout(ViewGroup.java:5817)
               at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
               at android.view.View.layout(View.java:18010)
               at android.view.ViewGroup.layout(ViewGroup.java:5817)
               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
               at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
               at android.view.View.layout(View.java:18010)
               at android.view.ViewGroup.layout(ViewGroup.java:5817)
               at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
               at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
               at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)

请帮忙,因为它没有让我知道错误来自哪里或类似的东西。

这是我的适配器类:

public class MyAppAdapter extends 

RecyclerView.Adapter<MyAppAdapter.ViewHolder>//has a class viewholder which holds
        
            private ArrayList<ClassListChat> mOriginalValues; // Original Values
            private ArrayList<ClassListChat> mDisplayedValues;

            public class ViewHolder extends RecyclerView.ViewHolder 

                TextView messageText;
                TextView messageUser;
                TextView messageTime;

                public ViewHolder(@NonNull View itemView) 
                    super(itemView);
                
            

            public List <ClassListChat> parkingList;

            public Context context;
            ArrayList<ClassListChat> arraylist;


            private MyAppAdapter(List<ClassListChat> apps, Context context) 
                this.parkingList = apps;
                this.context = context;
                arraylist = new ArrayList<ClassListChat>();
                arraylist.addAll(parkingList);
            



            @Override
            public MyAppAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) 
                View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listcontentstorechat,parent,false);
                ViewHolder viewHolder = new ViewHolder(rowView);
                if (rowView == null) 
                    LayoutInflater inflater = getLayoutInflater();
                    rowView = inflater.inflate(R.layout.listcontentstorechat, parent, false);
                    viewHolder.messageText = (BubbleTextView) rowView.findViewById(R.id.message_text);
                    viewHolder.messageUser = (TextView) rowView.findViewById(R.id.message_user);
                    viewHolder.messageTime = (TextView) rowView.findViewById(R.id.message_time);
                    rowView.setTag(viewHolder);
                 else 
                    viewHolder = (ViewHolder) rowView.getTag();
                
                // here setting up names and images




                return viewHolder;
            

            @Override
            public void onBindViewHolder(@NonNull ViewHolder holder, int position) 
                holder.messageText.setText(parkingList.get(position).getMessageText());
                holder.messageUser.setText(parkingList.get(position).getMessageUser());
                holder.messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", parkingList.get(position).getMessageTime()));
            

            @Override
            public int getItemCount() 
                return itemArrayList.size();
            
        


I would really appreciate your hard work in helping me!!

编辑:现在它只给了我一个空白的bubbleTextView(我在我的应用程序中实现了),但它没有显示文本、时间、用户也没有任何想法?

这是我的 listcontentstorechat.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_
    android:layout_>

    <TextView
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/message_user"
        android:textStyle="normal|bold"
        android:layout_
        android:layout_ />

    <TextView
        android:layout_alignParentBottom="@+id/message_user"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:id="@+id/message_time"
        android:layout_
        android:layout_ />

    <com.github.library.bubbleview.BubbleTextView
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp"
        android:layout_below="@id/message_user"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="#FFF"
        android:textSize="18sp"

        android:id="@+id/message_text"
        android:layout_
        android:layout_
        android:padding="10dp"
        app:arrowWidth="8dp"
        app:angle="8dp"
        app:arrowHeight="10dp"
        app:arrowPosition="14dp"
        app:arrowLocation="left"
        app:bubbleColor="#333639"
        app:arrowCenter="true"
        />

</RelativeLayout>

这是我的 ClassListChat.java

public class ClassListChat 
    public String messageText;
    public String messageUser;
    public long messageTime;


    public ClassListChat(String messageText, String messageUser) 
        this.messageText = messageText;
        this.messageUser = messageUser;
        messageTime = new Date().getTime();
    

    public ClassListChat() 
    

    public String getMessageText() 
        return messageText;
    

    public void setMessageText(String messageText) 
        this.messageText = messageText;
    

    public String getMessageUser() 
        return messageUser;
    

    public void setMessageUser(String messageUser) 
        this.messageUser = messageUser;
    

    public long getMessageTime() 
        return messageTime;
    

    public void setMessageTime(long messageTime) 
        this.messageTime = messageTime;
    

EDIT2:经过一些改进,它现在在 logcat 中返回错误:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.medicnes.ChatActivity$MyAppAdapter.onBindViewHolder(ChatActivity.java:275)
        at com.example.medicnes.ChatActivity$MyAppAdapter.onBindViewHolder(ChatActivity.java:225)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)

【问题讨论】:

【参考方案1】:

onCreateViewHolder() 中的ViewHolder 始终不为空,因为您在此方法中创建的View 不能为空。

onCreateViewHolder() 的构建方式与您用于创建ListView 适配器的getView() 不同

所以,请将您的onCreateViewHolder 更改为

        @Override
        public MyAppAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) 
            View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listcontentstorechat,parent,false);
            ViewHolder viewHolder = new ViewHolder(rowView);

            return viewHolder;
        

编辑: 现在(实际上之前)它不会将文本添加到回收视图中,对此有何帮助?我已经更新了我的问题

你从getItemCount()返回了错误的列表大小,因为你的适配器使用arraylist作为它的数据列表,所以你需要返回它的大小

所以把getItemCount()改成

@Override
 public int getItemCount() 
     if (arraylist != null)
         return arraylist.size();
     else
         return 0;
 

编辑 2:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.medicnes.ChatActivity$MyAppAdapter.onBindViewHolder(ChatActivity.java:275)
        at com.example.medicnes.ChatActivity$MyAppAdapter.onBindViewHolder(ChatActivity.java:225)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)

您没有使用.findViewById() 在行项目布局中初始化视图,因此将ViewHolder 构造函数更改为

public ViewHolder(@NonNull View itemView) 
    super(itemView);
    messageText = itemView.findViewById(R.id.message_text);
    messageUser = itemView.findViewById(R.id.message_user);
    messageTime = itemView.findViewById(R.id.message_time);

【讨论】:

谢谢,但现在(实际上之前)它没有将文本添加到回收视图中,对此有何帮助?我已经更新了我的问题! @VarroxSystems 感谢您的评论。请提供列表contentstorechat.xml @VarroxSystems 谢谢 .. 请检查更新的答案 我已经这样做了,但是现在它在logcat中返回错误一秒钟,我将更新问题 更新了问题,再次感谢您的辛勤工作!【参考方案2】:

您的适配器正在返回空视图。

View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listcontentstorechat,parent,false);

rowView 在这里总是非空的,所以这个条件:

if (rowView == null) 

永远不会被命中,而是代码转到 else 分支

 else 
    viewHolder = (ViewHolder) rowView.getTag();

viewHolder 替换为 null 并返回。

这部分代码看起来像是试图将旧的 ListView 适配器适配为 RecyclerView 适配器。使用 RecyclerView,您不需要 setTag()/getTag()

只需创建并初始化您的视图持有者并从方法中返回它。

【讨论】:

谢谢,但现在(实际上之前)它没有将文本添加到回收视图中,对此有何帮助?【参考方案3】:

我遇到了同样的问题,当我尝试在 Fragment 上使用 recyclerView 从 firebase firestore 检索数据并将其放入 recyclerView 时,我无法访问 recyclerView。只需在 findViewById(R.id.recyclerViewid) 前面添加getView()

我之前的代码:

//Declared at the top of the class
RecylcerView recyclerView ;
//In the onViewCreate section
recyclerView = findViewById(R.id.recyclerview)//<--id of the recyclerView

我的代码之后:

//Declared at the top of the class
RecylcerView recyclerView ;
//In the onViewCreate section
recyclerView = (getView).findViewById(R.id.recyclerview)//<--id of the recyclerView

【讨论】:

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

尝试从空对象引用上的字段“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)”