android.content.res.Resources$NotFoundException: 字符串资源 ID #0x1 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26001780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:55:47  来源:igfitidea点击:

android.content.res.Resources$NotFoundException: String resource ID #0x1 Error

androidexception

提问by Aneesh

I am using ListViewto dynamically add checkboxes in Android. I am using a ContextAdapterclass to add inflate the ListView. My error log is as follows:

我正在使用ListView在 Android 中动态添加复选框。我正在使用一个ContextAdapter类来添加膨胀ListView. 我的错误日志如下:

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)

ContextAdapterclass is as follows:

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

My ListDataclass:

我的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;
    }

}

Inside the onCreatefunction in the Activity I am doing this:

onCreateActivity的函数内,我正在这样做:

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

It would be of great help is someone could point out the mistake in my code. Thanks! Cheers!

有人可以指出我的代码中的错误,这将有很大帮助。谢谢!干杯!

回答by Orest Savchak

Instead of

代替

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

you should use

你应该使用

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

If you try to set integer as text, you call method setText(int resID) and application try to set as text some string resource with this resID.

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

回答by Ismael ozil

SetText doesnt allow integer values

SetText 不允许整数值

Just Add ""+for your code to look like this ""+rollNo.setText(items[position].getRollNo());

只需""+为您的代码添加如下所示""+rollNo.setText(items[position].getRollNo());

this error occurs because setText does not take Integers, it takes Strings

发生此错误是因为 setText 不采用整数,而是采用字符串

回答by Muhammad Ali

You can also use

你也可以使用

String.ValueOf()

in your code to avoid the Resource Not Found Exception .

在您的代码中避免 Resource Not Found Exception 。

Your code will look like this

您的代码将如下所示

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

It will work perfectly fine because the SetText() only requires stringsas values to be passed on.

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

回答by Wasi Sadman

This might help as well,

这也可能有帮助,

write like this -> textView.setText(""+cartModels.size());

像这样写-> textView.setText(""+cartModels.size());

instead of -> textView.setText(cartModels.size());

而不是 -> textView.setText(cartModels.size());

   TextView cartText = view.findViewById(R.id.cartTextDemo);

   CartViewModel cartViewModel = ViewModelProviders.of(this).get(CartViewModel.class);
   cartViewModel.getmAllCartsVM().observe(this, new Observer<List<CartModel>>() {
        @Override
        public void onChanged(@Nullable List<CartModel> cartModels) {

            if (cartModels != null) {
                //cartText.setText(cartModels.size()); // This will not work beacuse it is getting resource instead of string
                  cartText.setText(""+cartModels.size()); // this will give string
                Log.d(TAG, "onChanged: onChanged Called: CartModels Count: "+cartModels.size());
            }
        }
    });