微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java.lang.NullPointerException:尝试在空对象上调用虚方法“void android.widget.TextView.setText(java.lang.CharSequence)”引用

如何解决java.lang.NullPointerException:尝试在空对象上调用虚方法“void android.widget.TextView.setText(java.lang.CharSequence)”引用

我正在尝试制作一个简单的单屏应用程序来显示列表视图,但是发生了这个错误,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",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,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;
    }
}

Student.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;
    }
}

解决方法

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

改变这个

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

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

首先为 layout id 适配器设置正确的 ListView 以克服您目前拥有的 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 类型。

在 Adapter 的 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,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;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。