当前位置: 首页 > 知识库问答 >
问题:

RecyclerView:尝试调用虚拟方法void android。小装置。文本视图。setText(java.lang.CharSequence)'

宿衡虑
2023-03-14

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

**MyAdapter.java**

我是android中Recycler view的新手,我正在设计文本的样式,所以我做了如下的工作,这是我的适配器类。

    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.

它在加载时崩溃了,所以任何人都猜到出了什么问题。提前谢谢。

共有1个答案

桂坚
2023-03-14

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

因此,将视图通货膨胀替换为:

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

(使用R.layout.row,而不是R.layout.activity\u main)

 类似资料: