首页 > 解决方案 > 绝对应该初始化的 TextView 的 NullPointerException

问题描述

几周前,我开始在 Android Studio 中编码。我现在想学习如何使用 RecyclerViews 并尝试实现功能。我现在面临一些必须存在但不存在的 TextView 的麻烦。这是代码:

活动主.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="409dp"
        android:layout_height="729dp"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

项目.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"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fontFamily="sans-serif-black"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fontFamily="sans-serif-medium"
        android:text="TextView"
        android:textColor="@android:color/darker_gray"
        android:textSize="14dp" />

    <Button
        android:id="@+id/button"
        style="@android:style/Widget.DeviceDefault.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Abfrage"
        app:backgroundTint="@android:color/darker_gray" />
</LinearLayout>

MainActivity.java:

package com.example.recyclerviewexercise;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    RecyclerView contactsR;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        contactsR = findViewById(R.id.recyclerView);
        ArrayList<Contact> contacts = new ArrayList<Contact>();
        for (int i = 0; i < 20; i++) {contacts.add(new Contact());}
        contacts.add(new Contact());
        contactsR.setAdapter(new ListAdapter(contacts));
        contactsR.setLayoutManager(new LinearLayoutManager(this));
    }

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

        private ArrayList<Contact> contacts;

        public ListAdapter (ArrayList<Contact> contacts){
            this.contacts = (ArrayList<Contact>) contacts.clone();
        }

        public class ViewHolder extends RecyclerView.ViewHolder{
            private TextView nameV;
            private TextView descV;
            private Button btn;

            public ViewHolder (View itemView){
                super(itemView);
                nameV = itemView.findViewById(R.id.name);
                descV = itemView.findViewById(R.id.description);
                btn = itemView.findViewById(R.id.button);
            }

            public void setData(Contact contact){
                this.nameV.setText(contact.name);
                this.descV.setText(contact.description);
                /*try{
                    nameV.setText("Name");
                    descV.setText("contact.description");
                } catch(Exception e) {Log.i("setData", "failed: " + e.toString());}*/
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Context context = parent.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.items, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ListAdapter.ViewHolder holder, int position) {
            Log.i("bindHolder", "started " + Integer.toString(position));
            try{
                holder.setData(contacts.get(position));
                Log.i("bindHolder", "ended.");
            } catch (Exception e) {
                Log.i("bindHolder", "failed: " + e.toString());
            }

        }

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

联系方式.java:

package com.example.recyclerviewexercise;

public class Contact {
    public String name;
    public String description;
    private static int counter = 1;

    public Contact (){
        this.name = "Person " + Integer.toString(counter);
        this.description = "ist die " + Integer.toString(counter) + ". Person";
        counter++;
    }
}

和我开始时的日志:

2021-07-21 13:36:34.574 15222-15222/? I/zygote: Not late-enabling -Xcheck:jni (already on)
2021-07-21 13:36:34.579 15222-15222/com.example.recyclerviewexercise W/zygote: Unexpected CPU variant for X86 using defaults: x86
2021-07-21 13:36:34.668 15222-15252/com.example.recyclerviewexercise D/OpenGLRenderer: HWUI GL Pipeline
2021-07-21 13:36:34.708 15222-15252/com.example.recyclerviewexercise I/OpenGLRenderer: Initialized EGL, version 1.4
2021-07-21 13:36:34.708 15222-15252/com.example.recyclerviewexercise D/OpenGLRenderer: Swap behavior 1
2021-07-21 13:36:34.709 15222-15252/com.example.recyclerviewexercise W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-07-21 13:36:34.709 15222-15252/com.example.recyclerviewexercise D/OpenGLRenderer: Swap behavior 0
2021-07-21 13:36:34.721 15222-15252/com.example.recyclerviewexercise D/EGL_emulation: eglCreateContext: 0xa23850c0: maj 3 min 0 rcv 3
2021-07-21 13:36:34.742 15222-15252/com.example.recyclerviewexercise D/EGL_emulation: eglMakeCurrent: 0xa23850c0: ver 3 0 (tinfo 0xa2383210)
2021-07-21 13:36:34.744 15222-15252/com.example.recyclerviewexercise E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2021-07-21 13:36:34.744 15222-15252/com.example.recyclerviewexercise E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2021-07-21 13:36:34.744 15222-15252/com.example.recyclerviewexercise E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2021-07-21 13:36:34.744 15222-15252/com.example.recyclerviewexercise E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2021-07-21 13:36:34.770 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 0
2021-07-21 13:36:34.770 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.793 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 1
2021-07-21 13:36:34.793 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.809 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 2
2021-07-21 13:36:34.809 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.824 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 3
2021-07-21 13:36:34.824 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.839 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 4
2021-07-21 13:36:34.840 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.851 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 5
2021-07-21 13:36:34.851 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.856 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 6
2021-07-21 13:36:34.856 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.865 15222-15233/com.example.recyclerviewexercise I/zygote: Do partial code cache collection, code=10KB, data=20KB
2021-07-21 13:36:34.866 15222-15233/com.example.recyclerviewexercise I/zygote: After code cache collection, code=10KB, data=20KB
2021-07-21 13:36:34.866 15222-15233/com.example.recyclerviewexercise I/zygote: Increasing code cache capacity to 128KB
2021-07-21 13:36:34.867 15222-15233/com.example.recyclerviewexercise I/zygote: Do partial code cache collection, code=10KB, data=37KB
2021-07-21 13:36:34.867 15222-15233/com.example.recyclerviewexercise I/zygote: After code cache collection, code=10KB, data=37KB
2021-07-21 13:36:34.867 15222-15233/com.example.recyclerviewexercise I/zygote: Increasing code cache capacity to 256KB
2021-07-21 13:36:34.868 15222-15233/com.example.recyclerviewexercise I/zygote: JIT allocated 71KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2021-07-21 13:36:34.868 15222-15233/com.example.recyclerviewexercise I/zygote: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
2021-07-21 13:36:34.870 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 7
2021-07-21 13:36:34.870 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.886 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 8
2021-07-21 13:36:34.886 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.897 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 9
2021-07-21 13:36:34.897 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.907 15222-15233/com.example.recyclerviewexercise I/zygote: Do full code cache collection, code=86KB, data=54KB
2021-07-21 13:36:34.907 15222-15233/com.example.recyclerviewexercise I/zygote: After code cache collection, code=84KB, data=40KB
2021-07-21 13:36:34.908 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 10
2021-07-21 13:36:34.909 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.921 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 11
2021-07-21 13:36:34.921 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.933 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 12
2021-07-21 13:36:34.933 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.942 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 13
2021-07-21 13:36:34.942 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.946 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 14
2021-07-21 13:36:34.946 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.951 15222-15222/com.example.recyclerviewexercise I/bindHolder: started 15
2021-07-21 13:36:34.951 15222-15222/com.example.recyclerviewexercise I/bindHolder: ended.
2021-07-21 13:36:34.968 15222-15252/com.example.recyclerviewexercise D/EGL_emulation: eglMakeCurrent: 0xa23850c0: ver 3 0 (tinfo 0xa2383210)

我希望,你能帮忙。

标签: javaandroidxmlandroid-layoutandroid-recyclerview

解决方案


您的 textview 名称在 items.xml 中,您试图在 activity_main.xml 中找到它,nameV = (TextView)items.findViewById(R.id.name);对 desV 和 btn 尝试相同。


推荐阅读