Я новичок Android Разработчик и столкнулся с проблемой из-за сбоя приложения во время выполнения. Пожалуйста, проверьте код ниже.
**MyAdapter.java**
Я новичок в представлении Recycler в android, и я работаю над стилизацией текста, поэтому я сделал следующее: мой класс адаптера.
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);
}
}}
Исключение исходит от onBindViewHolder (): team1Tv = itemView.findViewById (R.id.team1TV);
**Row.xml**
Это пользовательский 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.
Сбой при загрузке Так что кто-нибудь догадывается, что пошло не так. Заранее спасибо.