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

时间:2014-09-23 18:02:59

标签: android exception

我正在使用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)

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

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

4 个答案:

答案 0 :(得分:123)

而不是

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

你应该使用

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

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

答案 1 :(得分:3)

SetText不允许整数值

只需为您的代码添加""+即可""+rollNo.setText(items[position].getRollNo());

发生此错误是因为setText不接受整数,需要使用字符串

答案 2 :(得分:0)

您也可以使用

String.ValueOf()
在代码中

以避免资源未找到异常。

您的代码将如下所示

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

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

答案 3 :(得分:-1)

这可能也有帮助,

这样写-> 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());
            }
        }
    });