gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:13:06 25 4
gpt4 key购买 nike

我正在制作一个应用程序,它从 sql 获取数据并将其放入回收 View 中回收 View 它在 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_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>

这是我的 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)

最佳答案

ViewHolderonCreateViewHolder() 中始终不为空作为 View您在此方法中创建的不能为空。
onCreateViewHolder()以不同于 getView() 的方式构建用于创建 ListView适配器

所以,请更改您的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



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

所以改变 getItemCount()进入
@Override
public int getItemCount() {
if (arraylist != null)
return arraylist.size();
else
return 0;
}

Edit 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() 在行项目布局中初始化 View , 所以改变 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);
}

关于java - 尝试从空对象引用上的字段 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61375259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com