gpt4 book ai didi

java - 回收器 View : Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

转载 作者:行者123 更新时间:2023-12-02 08:58:19 25 4
gpt4 key购买 nike

我是新的 Android 开发人员,由于应用程序在运行时崩溃而面临问题。请检查下面的代码。

**MyAdapter.java**

I am new to Recycler view in android and I am working on styling the text so I have done like that below is my adapter class.

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

//variables
private List<Model> modelList;
private Context context;

//constructor
public MyAdapter(List<Model> modelList, Context context) {
this.modelList = modelList;
this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//this method will call whenever our view holder is created
// inflate the row.xml
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, parent, false);
if(view == null){
Log.i(view.toString(), "View is null");
}else{
Log.i(view.toString(), "View is not null");
}
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// this method will bind the data to viewHolder from when it will be shown to other view
Model model = modelList.get(position);
if(holder.team1Tv == null){
Log.i("model.getTeam1() -->",model.getTeam1());
}else{
Log.i("model.getTeam2() -->",model.getTeam1());
}
holder.team1Tv.setText(model.getTeam1());
holder.team2Tv.setText(model.getTeam2());
holder.matchTypeTv.setText(model.getMatchType());
holder.matchStatusTv.setText(model.getMatchStatus());
holder.dateTv.setText(model.getDate());

//handle item clicks
holder.cardView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//will do later
}
});
}

@Override
public int getItemCount() {
return modelList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

//define view objects
TextView team1Tv, team2Tv, matchTypeTv, matchStatusTv, dateTv;
CardView cardView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
// intialize the view objects
team1Tv = itemView.findViewById(R.id.team1TV);
team2Tv = itemView.findViewById(R.id.team2TV);
matchTypeTv = itemView.findViewById(R.id.matchTypeTV);
matchStatusTv = itemView.findViewById(R.id.matchStatusTV);
dateTv = itemView.findViewById(R.id.dateTV);
cardView = itemView.findViewById(R.id.cardView);
}
}}

Exception is coming from onBindViewHolder(): team1Tv = itemView.findViewById(R.id.team1TV);

**Row.xml**

This is custom xml created to show list of tiles.

    <?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cardView"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="3dp"
app:cardElevation="3dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:stretchColumns="2">

<TableRow>
<TextView
android:text="Team1"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"/>
<TextView
android:id="@+id/team1TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Team2"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"/>
<TextView
android:id="@+id/team2TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Match Type"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"/>
<TextView
android:id="@+id/matchTypeTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Match Status"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"/>
<TextView
android:id="@+id/matchStatusTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Date"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"/>
<TextView
android:id="@+id/dateTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</androidx.cardview.widget.CardView>

----------------
**activity_main.xml**

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

**Error logs from LogCat:**

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sportsx, PID: 20974
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.sportsx.MyAdapter.onBindViewHolder(MyAdapter.java:53)
at com.example.sportsx.MyAdapter.onBindViewHolder(MyAdapter.java:18)
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)
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:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:1041)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3382)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2849)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1897)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8516)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:986)
at android.view.Choreographer.doCallbacks(Choreographer.java:764)
at android.view.Choreographer.doFrame(Choreographer.java:699)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:965)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7099)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
I/Process: Sending signal. PID: 20974 SIG: 9
Process 20974 terminated.

加载时崩溃所以大家猜猜出了什么问题。提前致谢。

最佳答案

您在 onCreateViewHolder 方法中为 RecyclerView 项指定了不正确的布局。

因此,将 View 膨胀替换为:

 View view  = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent,  false);

(使用R.layout.row而不是R.layout.activity_main)

关于java - 回收器 View : Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60353770/

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