【问题标题】:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object refer [duplicate]java.lang.NullPointerException:尝试在空对象上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'参考[重复]
【发布时间】:2021-07-15 15:46:07
【问题描述】:

我正在尝试制作一个简单的单屏应用程序来显示列表视图,但发生了这个错误,ReportCard 是 ArrayAdapter 类。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity" />

MainActivity.java

package com.vitikasoni.reportcard;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<Student> s = new ArrayList<Student>();
        s.add(new Student("Vitika", 87, 79, 93, 89));
        s.add(new Student("Faizal", 90, 95, 89, 99));
        s.add(new Student("Prince", 83, 73, 92, 67));
        s.add(new Student("Sejal", 83, 79, 95, 79));

        ReportCard r = new ReportCard(this, s);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(r);
    }
}

view_view.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/na"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/m"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/e"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="English:98" />
    </LinearLayout>

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/ev"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="EVS:98" />

        <TextView
            android:id="@+id/h"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Hindi:98" />

        <TextView
            android:id="@+id/p"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Percent:98%" />
    </LinearLayout>
 </LinearLayout>

Reportcard.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class ReportCard extends ArrayAdapter<Student> {
    public ReportCard(Context c, ArrayList<Student> s) {
        super(c, 0, s);
    }

    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.activity_main, parent, false);
        }
        Student currentWord = getItem(position);

        TextView na = (TextView) listItemView.findViewById(R.id.na);
        na.setText(currentWord.getName());

        TextView m = (TextView) listItemView.findViewById(R.id.m);
        m.setText(currentWord.getMaths());

        TextView h = (TextView) listItemView.findViewById(R.id.h);
        h.setText(currentWord.getHindi());

        TextView e = (TextView) listItemView.findViewById(R.id.e);
        e.setText(currentWord.getEng());

        TextView ev = (TextView) listItemView.findViewById(R.id.ev);
        ev.setText(currentWord.getEvs());

        TextView p = (TextView) listItemView.findViewById(R.id.p);

        return listItemView;
    }
}

学生.java

package com.vitikasoni.reportcard;

public class Student {
    private String name;
    private int eng;
    private int maths;
    private int hindi;
    private int evs;
    private double per;

    public Student(String n, int e, int m, int h, int ev) {
        name = n;
        eng = e;
        maths = m;
        hindi = h;
        evs = ev;
    }

    public String getName() {
        return name;
    }

    public int getEng() {
        return eng;
    }

    public int getHindi() {
        return hindi;
    }

    public int getEvs() {
        return evs;
    }

    public int getMaths() {
        return maths;
    }

    public double getPer() {
        double pr = (eng + hindi + maths + evs) / 4;
        return pr;
    }
}

【问题讨论】:

    标签: java android xml android-arrayadapter virtual


    【解决方案1】:

    您将错误的布局设置为适配器项目视图。

    改变这个

    listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
    

    listItemView = LayoutInflater.from(getContext()).inflate(R.layout.view_view, parent, false);
    

    【讨论】:

    • 修改后会崩溃!!! android.content.res.Resources$NotFoundException: 字符串资源 ID #0x4f
    • @NRUSINGHAMOHARANA 代码中可能存在其他问题,因为 OP 没有提供任何堆栈跟踪。但是,需要更改布局,因为您可以看到适配器类中使用的所有参考 OP 都来自布局 view_view.xml 文件。
    • 是的,我明白了。我想说的是,您的解决方案绝对是解决 NPE,但之后应用程序将无法运行。冷静点兄弟。我已成功运行代码并更新了我的答案。
    【解决方案2】:

    首先为ListView 适配器设置正确的layout id,以克服您当前拥有的Null Pointer Exception 。修复此问题后,您的代码将无法运行,因为还有另一个问题,您肯定会得到 android.content.res.Resources$NotFoundException: String resource ID #0x4f 异常。

    您的setText(currentWord.getMaths()) 行有问题,因为这里currentWord.getMaths()integer 类型,而setText(integer) 将此interger 参数视为R.string.someName

    所以要么将 private int maths; 更改为 private String maths; 您必须将所有 interger 类型更改为 String 类型。

    在适配器的getView() 方法中使用以下代码。

    // 这里我将integer 连接到String 并设置为TextView

            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View listItemView = convertView;
            if (listItemView == null) {
                listItemView = LayoutInflater.from(getContext()).inflate(
                        R.layout.view_view, parent, false);
            }
            Student currentWord = getItem(position);
    
            TextView na = (TextView) listItemView.findViewById(R.id.na);
            na.setText(currentWord.getName());
    
            TextView m = (TextView) listItemView.findViewById(R.id.m);
            m.setText(currentWord.getMaths()+"");
    
            TextView h = (TextView) listItemView.findViewById(R.id.h);
            h.setText(currentWord.getHindi()+"");
    
            TextView e = (TextView) listItemView.findViewById(R.id.e);
            e.setText(currentWord.getEng()+"");
    
            TextView ev = (TextView) listItemView.findViewById(R.id.ev);
            ev.setText(currentWord.getEvs()+"");
    
            TextView p = (TextView) listItemView.findViewById(R.id.p);
    
            return listItemView;
        }
    

    【讨论】:

    • 非常感谢!!! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多