java.lang.nullpointerexception:尝试对空对象调用虚拟方法“void android.widget.textview.settext(java.lang.charsequence)”

gorkyyrv  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(454)

这个问题在这里已经有答案了

什么是nullpointerexception,如何修复它(12个答案)
三天前关门了。
我试图使一个单一的屏幕显示listview简单的应用程序,但发生了这个错误,reportcard是arrayadapter类。
活动\u 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" />

主活动.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);
    }
}

查看\u 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>

报表卡.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;
    }
}
dohp0rv5

dohp0rv51#

第一组正确 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 类型。

在中使用以下代码 getView() 适配器的方法。
//在这里我正在浓缩 integerString 并设置为 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;
}
2w3kk1z5

2w3kk1z52#

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

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

具有

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

相关问题