使用setText时未找到资源异常

时间:2014-08-22 05:47:49

标签: android

我第一次只遇到这个错误所以我真的不知道如何修复它。我认为我的问题在于使用setText方法。我不知道为什么。在修改我的一些代码之前,我的代码完全正常。现在我收到了这个错误。以下是我的代码,

MyAdapter.java

package com.functions;

import java.util.ArrayList;
import java.util.List;

import com.example.senditem.R;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MyAdapter extends ArrayAdapter<Model> {

 private final List<Model> list;

 String adapter;
    private final Activity context;
    private final int id;
    int listPosition;
    public MyAdapter(Activity context, List<Model> list, int id){
        super(context, R.layout.userproduct, list);
        this.context = context;
        this.list = list;
        this.id = id;
    }


    class ViewHolder{
        protected TextView text;
        EditText quantity;
        protected TextView amount;
        protected Spinner uom;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        listPosition = position;
        ViewHolder viewHolder = null;
        if(convertView == null){
            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.userproduct, null);
            viewHolder = new ViewHolder(); 
            viewHolder.text = (TextView) convertView.findViewById(R.id.label);
            viewHolder.quantity = (EditText) convertView.findViewById(R.id.inputQty);
            viewHolder.amount = (TextView) convertView.findViewById(R.id.inputAmt);
            viewHolder.uom = (Spinner) convertView.findViewById(R.id.inputUom);
            convertView.setTag(viewHolder);
            convertView.setTag(R.id.label, viewHolder.text);
            convertView.setTag(R.id.inputQty, viewHolder.quantity);
            convertView.setTag(R.id.inputAmt, viewHolder.amount);
            convertView.setTag(R.id.inputUom, viewHolder.uom);
        }
        else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SqliteHelper db = new SqliteHelper(context);
        viewHolder.text.setText(list.get(position).getName());
        viewHolder.quantity.setId(position);
        viewHolder.amount.setText(String.valueOf(Model.getAmt()));
        viewHolder.amount.setId(position);
        viewHolder.uom.setId(position);
        viewHolder.text.setId(position);
        String data = db.getAllDetails(id,Model.getitemId());
        if(data==null)
        {
             viewHolder.quantity.setText(list.get(position).quantity);

        }
        else{
            viewHolder.quantity.setText(data);
        }
        viewHolder.quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus){
                        final int position = v.getId();
                        final EditText Caption = (EditText) v;



     list.get(position).quantity = Integer.parseInt(Caption.getText().toString());
                    }
                }
            });


        List<String> uomlist = new ArrayList<String>();

        for(int j=0;j<list.size();j++){
            String itemid = list.get(j).getitemId(); 
            uomlist.add(db.selectUom(itemid));

            ArrayAdapter<String> uomadapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item,uomlist);
            uomadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            viewHolder.uom.setAdapter(uomadapter);

        }


        return convertView;

    }

 }

userproduct.xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >



<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="29dp"
    android:text="Item"
    android:textSize="15dp" />

<TextView
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/my_list"
    android:layout_alignParentRight="true"
    android:layout_marginRight="14dp"
    android:text="Amount"
    android:textSize="15dp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/my_list"
    android:layout_marginRight="14dp"
    android:layout_toLeftOf="@+id/TextView02"
    android:text="UOM"
    android:textSize="15dp" />

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Quantity"
    android:textSize="15dp" />

<TextView
    android:id="@+id/inputiName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/label" />

<EditText
    android:id="@+id/inputQty"
    android:layout_width="40dp"
    android:layout_height="35dp"
    android:layout_alignTop="@+id/inputUom"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:gravity="right"
    android:inputType="number"
    android:textSize="12sp" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/inputAmt"
    android:layout_width="50dp"
    android:layout_height="35dp"
    android:layout_below="@+id/TextView02"
    android:layout_marginTop="16dp"
    android:layout_toRightOf="@+id/textView2"
    android:ems="10"
    android:gravity="right"
    android:inputType="number"
    android:textSize="12sp" />

<Spinner
    android:id="@+id/inputUom"
    android:layout_width="40dp"
    android:layout_height="35dp"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignTop="@+id/inputAmt" />

<TextView
    android:id="@+id/label"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/inputQty"
    android:layout_alignParentLeft="true"
    android:textSize="15sp" />

</RelativeLayout>

logcat的

08-22 13:45:13.355: E/AndroidRuntime(10940): FATAL EXCEPTION: main
08-22 13:45:13.355: E/AndroidRuntime(10940): android.content.res.Resources$NotFoundException: String resource ID #0x0
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.content.res.Resources.getText(Resources.java:320)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.TextView.setText(TextView.java:3797)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at com.functions.MyAdapter.getView(MyAdapter.java:76)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.AbsListView.obtainView(AbsListView.java:2207)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.ListView.makeAndAddView(ListView.java:1877)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.ListView.fillDown(ListView.java:710)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.ListView.fillFromTop(ListView.java:771)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.ListView.layoutChildren(ListView.java:1693)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.AbsListView.onLayout(AbsListView.java:2037)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.View.layout(View.java:14118)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.ViewGroup.layout(ViewGroup.java:4467)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.View.layout(View.java:14118)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.ViewGroup.layout(ViewGroup.java:4467)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.View.layout(View.java:14118)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.ViewGroup.layout(ViewGroup.java:4467)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.View.layout(View.java:14118)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.ViewGroup.layout(ViewGroup.java:4467)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.View.layout(View.java:14118)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.ViewGroup.layout(ViewGroup.java:4467)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2183)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1947)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.Choreographer.doCallbacks(Choreographer.java:579)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.Choreographer.doFrame(Choreographer.java:548)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.os.Handler.handleCallback(Handler.java:800)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.os.Handler.dispatchMessage(Handler.java:100)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.os.Looper.loop(Looper.java:194)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at android.app.ActivityThread.main(ActivityThread.java:5400)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at java.lang.reflect.Method.invoke(Method.java:525)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
08-22 13:45:13.355: E/AndroidRuntime(10940):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

你的错误应该在这一行:

    if(data==null)
    {
       //  viewHolder.quantity.setText(list.get(position).quantity); <---ERROR HERE
       //  Probably quantity is not a string, so use String.valueOf Function here
           viewHolder.quantity.setText(String.valueOf(list.get(position).quantity));

    }
    else{
        // data is already declared a string. So, String.valueOf is not required.
        viewHolder.quantity.setText(data);
    }

根据答案here setText with string显示这样的值,而setText with int则试图找到与int值匹配的资源。