You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义ArrayAdapter适配整数类型失效,应用安装AVD后无法启动求助

问题原因与解决方案

这个问题其实是TextView的setText()方法重载带来的坑!当你把data类里的字段从String改成Integer后,在Adapter里直接调用textView.setText(currentItem.getXXX())时,系统会触发接受int参数的重载版本——这个int参数会被当成Android资源ID(比如R.string.app_name这类)去查找,但你传入的数字(比如2、6)肯定不是有效的资源ID,所以会抛出Resources$NotFoundException,直接导致App崩溃打不开。

修复步骤:

我们需要把Integer类型的数值转换成字符串再传递给setText(),下面是修改后的完整代码:

1. 修改data.java(字段改为Integer类型)

public class data {
    private Integer confirmed;
    private Integer dead;
    private String country;
    private Integer recovered;
    private Integer updated;

    // Getter方法对应改为返回Integer
    public Integer getConfirmed() {
        return confirmed;
    }
    public Integer getDead() {
        return dead;
    }
    public Integer getRecovered() {
        return recovered;
    }
    public String getCountry() {
        return country;
    }
    public Integer getUpdated() {
        return updated;
    }

    // 构造方法参数对应调整
    public data(Integer confirmed, Integer dead, String country, Integer recovered, Integer updated){
        this.confirmed = confirmed;
        this.dead = dead;
        this.country = country;
        this.recovered = recovered;
        this.updated = updated;
    }
}

2. 修改adapter.java(Integer转String后再设置文本)

在getView()方法里,把每个Integer类型的字段转换成字符串,推荐用String.valueOf()方法(最安全,能避免空指针问题):

public class adpater extends ArrayAdapter<data> {
    public adpater(Activity context, List<data> arrayList) {
        super(context,0, arrayList);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if(convertView==null){
            convertView= LayoutInflater.from(getContext()).inflate(R.layout.list,parent,false);
        }
        data currentitem = getItem(position);
        
        TextView textView=convertView.findViewById(R.id.location);
        TextView textView1=convertView.findViewById(R.id.recovered);
        TextView textView2=convertView.findViewById(R.id.dead);
        TextView textView3=convertView.findViewById(R.id.confirmed);
        TextView textView4=convertView.findViewById(R.id.updated);

        // 字符串类型直接设置
        textView.setText(currentitem.getCountry());
        // Integer类型转成String再设置
        textView1.setText(String.valueOf(currentitem.getRecovered()));
        textView2.setText(String.valueOf(currentitem.getDead()));
        textView3.setText(String.valueOf(currentitem.getConfirmed()));
        textView4.setText(String.valueOf(currentitem.getUpdated()));

        return convertView;
    }
}

3. 修改MainActivity.java(构造参数改为整数)

因为data的构造方法现在接受Integer类型参数,所以初始化的时候要传整数而不是字符串:

package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ArrayList<data> arr= new ArrayList<>();
        // 这里传递整数,不再是字符串
        arr.add(new data(2,6,"india",4,22222));
        
        ListView listView=findViewById(R.id.list);
        adpater a=new adpater(this,arr);
        listView.setAdapter(a);
    }
}

额外提示:

以后遇到App崩溃的情况,一定要先看Logcat里的错误信息——你会看到类似Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2的报错,这直接就能定位到是setText传了无效资源ID的问题。

内容的提问来源于stack exchange,提问作者rahim virani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:05:33