web-dev-qa-db-fra.com

android.content.res.Resources $ NotFoundException: Erreur d'ID de ressource de chaîne # 0x1

J'utilise ListView pour ajouter dynamiquement des cases à cocher dans Android. J'utilise une classe ContextAdapter pour ajouter gonfler le ListView. Mon journal des erreurs est le suivant:

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 classe est la suivante:

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

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

}

Dans la fonction onCreate de l'activité que je fais:

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

Ce serait d'une grande aide si quelqu'un pouvait signaler l'erreur dans mon code. Merci! À votre santé!

32
Aneesh

Au lieu de

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

tu devrais utiliser

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

Si vous essayez de définir un entier comme texte, vous appelez la méthode setText (int resID) et l'application essaie de définir comme texte une ressource de chaîne avec ce resID.

131
Orest Savchak

SetText n'autorise pas les valeurs entières

Ajoutez simplement ""+ Pour que votre code ressemble à ceci ""+rollNo.setText(items[position].getRollNo());

cette erreur se produit car setText ne prend pas des entiers, il prend des chaînes

4
Ismael ozil