[英]RecyclerView: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
I am new Android Developer and facing problem because of app is crashing at runtime.我是新的 Android 开发人员,由于应用程序在运行时崩溃而面临问题。 Please check the code below.
请检查下面的代码。
**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.
我是 android 中 Recycler 视图的新手,我正在设计文本样式,所以我已经像下面那样做了我的适配器 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);
异常来自 onBindViewHolder():team1Tv = itemView.findViewById(R.id.team1TV);
**Row.xml**
This is custom xml created to show list of tiles.
这是为显示图块列表而创建的自定义 xml。
<?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.
Its crashing while loading So anybody guess what went wrong.它在加载时崩溃所以任何人都猜猜出了什么问题。 Thanks in advance.
提前致谢。
You specify incorrect layout for the RecyclerView items in onCreateViewHolder
method.您在
onCreateViewHolder
方法中为 RecyclerView 项目指定了不正确的布局。
So, replace view inflation with:因此,将 view inflation 替换为:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
(use R.layout.row
and not R.layout.activity_main
) (使用
R.layout.row
而不是R.layout.activity_main
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.