简体   繁体   English

尝试从字段 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' 读取 null object 参考

[英]Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' on a null object reference

I am making an app that gets data from sql and puts it in a recycle view i first tried it in a list view but i found it easier in a recycle view it is basically like a chat system app anyways,when i add my data into the recycle view it gives me this error in the logcat.我正在制作一个从 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)

Please help because it doesn't give me any clue on where is the error coming from or stuff like that.请帮忙,因为它没有给我任何关于错误来自何处或类似问题的线索。

here is my adapter class:这是我的适配器 class:

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!!

EDIT: now it just gives me a blank bubbleTextView(which i implemented into my app)but it doesn't show the text,nor the time,nor the user any ideas?编辑:现在它只给我一个空白的 bubbleTextView(我在我的应用程序中实现了它)但它不显示文本,也不显示时间,也不显示用户的任何想法?

Here is my listcontentstorechat.xml:这是我的 listcontentstorechat.xml:

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

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

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

    <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_width="wrap_content"
        android:layout_height="wrap_content"
        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>

And here is my ClassListChat.java这是我的 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: After some improvements it now returns an error in the logcat saying: 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)

The ViewHolder is always not null in onCreateViewHolder() as the View you created within this method can't be null. onCreateViewHolder()中的ViewHolder始终不是 null,因为您在此方法中创建的View不能是 null。

onCreateViewHolder() is built in a different way than getView() that you use to create ListView adapter onCreateViewHolder()的构建方式与用于创建ListView适配器的getView()不同

So, please Change your onCreateViewHolder to be所以,请将您的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;
        }

Edit: now (and before actually) it doesn't add the text into the recycle view any help for that?编辑:现在(实际上之前)它不会将文本添加到回收视图中有什么帮助吗? I have updated my question我更新了我的问题

You return a wrong list size from the getItemCount() , as your adapter uses arraylist as its list of data, so you need to return the size of it您从getItemCount()返回错误的列表大小,因为您的适配器使用arraylist作为其数据列表,因此您需要返回它的大小

So change getItemCount() into所以把getItemCount()改成

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

Edit 2:编辑 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)

You didn't initialize views within the row item layout using .findViewById() , so change ViewHolder constructor to be您没有使用.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);
}

Your adapter is returning null viewholders.您的适配器返回 null viewholders。

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

rowView is always nonnull here, so this condition: rowView在这里总是非空的,所以这个条件:

 if (rowView == null) {

is never hit and instead the code goes to the else branch永远不会被击中,而是代码转到 else 分支

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

that replaces viewHolder with null and you return that.viewHolder替换 viewHolder 然后你返回它。

That part of the code looks like an attempt to adapt and older ListView adapter to be a RecyclerView adapter.这部分代码看起来像是尝试将旧的 ListView 适配器改编为 RecyclerView 适配器。 You don't need the setTag() / getTag() yourself with RecyclerView.对于 RecyclerView,您自己不需要setTag() / getTag()

Just create and init your view holder and return that from the method.只需创建并初始化您的视图持有者并从方法中返回它。

I had the same issue, when I tried to use recyclerView on a Fragment to retrieve the data from firebase firestore and place it in the recyclerView , I was not able to access the recyclerView .我遇到了同样的问题,当我尝试在Fragment上使用recyclerView从 firebase firestore 检索数据并将其放入recyclerView时,我无法访问recyclerView Just add getView() in front of findViewById(R.id.recyclerViewid)只需在 findViewById(R.id.recyclerViewid) 前面添加getView() )

My Code Before:我之前的代码:

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

My Code After:我的代码之后:

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

This is may be because your are not returning view through binding in setcontentview这可能是因为您没有通过 setcontentview 中的绑定返回视图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从 null object 参考上的字段 '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 void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager) 在 null object 参考 - void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager) on a null object reference 如何修复在 null object 参考上调用虚拟方法 'android.view.View androidx.fragment.app.FragmentActivity.findViewById(int)' 的尝试? - How to fix attempt to invoke virtual method 'android.view.View androidx.fragment.app.FragmentActivity.findViewById(int)' on a null object reference? 遇到在 null object 参考上调用虚拟方法 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' 的尝试 - Encountering a Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference Android:尝试在 null object 引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)” - Android:Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference - Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference 尝试在空对象引用上调用虚拟方法“void android.widget.RadioGroup.addView(android.view.View)” - Attempt to invoke virtual method 'void android.widget.RadioGroup.addView(android.view.View)' on a null object reference 尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)” - Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference Android ViewGroup 崩溃:尝试从空对象引用上的字段“int android.view.View.mViewFlags”读取 - Android ViewGroup crash: Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference Android错误:尝试在空对象引用上调用虚拟方法……setOnClickListener(android.view.View $ OnClickListener)&#39; - Android Error: Attempt to invoke virtual… setOnClickListener(android.view.View$OnClickListener)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM