当前位置: 首页 > 知识库问答 >
问题:

Android所容纳之物res.Resources$NotFoundException:字符串资源ID#0x1错误[重复]

谷梁楚青
2023-03-14

我正在使用ListView在Android中动态添加复选框。我正在使用一个ContextAdapter类来添加充气列表视图。我的错误日志如下:

09-23 13:44:45.000: E/AndroidRuntime(1192): FATAL EXCEPTION: main
09-23 13:44:45.000: E/AndroidRuntime(1192): Process: com.project.attendancemanager, PID: 1192
09-23 13:44:45.000: E/AndroidRuntime(1192): android.content.res.Resources$NotFoundException: String resource ID #0x1
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.content.res.Resources.getText(Resources.java:244)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.widget.TextView.setText(TextView.java:3888)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at com.project.attendancemanager.CustomAdapter.getView(CustomAdapter.java:31)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.widget.ListView.onMeasure(ListView.java:1175)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.View.measure(View.java:16497)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.View.measure(View.java:16497)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.View.measure(View.java:16497)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.View.measure(View.java:16497)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.View.measure(View.java:16497)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.Choreographer.doFrame(Choreographer.java:544)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.os.Handler.handleCallback(Handler.java:733)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.os.Looper.loop(Looper.java:136)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at java.lang.reflect.Method.invokeNative(Native Method)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at java.lang.reflect.Method.invoke(Method.java:515)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-23 13:44:45.000: E/AndroidRuntime(1192):     at dalvik.system.NativeStart.main(Native Method)

ContextAdapter类如下:

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

public class CustomAdapter extends ArrayAdapter<ListData>{

    ListData[] items = null;
    Context context;

    public CustomAdapter(Context context, ListData[] resource) {
        super(context, R.layout.activity_list_row, resource);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.items = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflator = ((Activity) context).getLayoutInflater();
        convertView = inflator.inflate(R.layout.activity_list_row, parent, false);
        TextView name = (TextView) convertView.findViewById(R.id.NameTV);
        TextView rollNo = (TextView) convertView.findViewById(R.id.RollNoTV);
        CheckBox cb = (CheckBox) convertView.findViewById(R.id.studentCB);
        name.setText(items[position].getName());
        rollNo.setText(items[position].getRollNo());

        if(items[position].getValue() == 1)
            cb.setChecked(true);
        else
            cb.setChecked(false);

        return convertView;
    }   
}

我的ListData类:

package com.project.attendancemanager;

public class ListData {

    String name;
    int value, rollno;

    ListData(String name, int rollno, int value){
        this.name = name;
        this.rollno = rollno;
        this.value = value;
    }

    public String getName(){
        return this.name;
    }

    public int getRollNo(){ 
        return this.rollno;
    }

    public int getValue(){  
        return this.value;
    }

}

在活动中的onCreate函数中,我正在执行以下操作:

Listview lv;
ListData[] items;
CustomAdapter adapter = new CustomAdapter(this, items);
lv.setAdapter(adapter);

如果有人能指出我代码中的错误,那将非常有帮助。谢谢干杯

共有3个答案

朱俊雅
2023-03-14

您也可以使用

String.valueOf()

在您的代码中,以避免资源未找到异常。

您的代码将如下所示

rollNo.setText(String.valueOf(items[position].getRollNo()));

它可以很好地工作,因为SetText()只需要字符串作为传递的值。

苏华荣
2023-03-14

SetText不允许整数值

只需添加代码,使您的代码看起来像这样。setText(items[position].getRollNo())

发生此错误的原因是setText不接受整数,而是接受字符串

徐旻
2023-03-14

而不是

rollNo.setText(items[position].getRollNo());

你应该使用

rollNo.setText(Integer.toString(items[position].getRollNo()));

如果您尝试将整数设置为文本,则调用方法setText(int resID)并应用程序尝试将具有此resID的某些字符串资源设置为文本。

 类似资料: