为什么在填充自定义列表视图时出现错误?

时间:2014-05-28 11:23:35

标签: android android-layout listview

我正在使用自定义列表视图,我收到了下一个错误。为什么??在日志中我很好地接收了数据,但由于某种原因它崩溃了。

public class ListViewCustomCloseit extends BaseAdapter {

    ArrayList<CloseItListModel> closeItListModelArrayList;

    private LayoutInflater mInflater;

    public ListViewCustomCloseit(Context context, ArrayList<CloseItListModel> results) {
        closeItListModelArrayList = results;
        mInflater = LayoutInflater.from(context);

        for(int i=0; i < closeItListModelArrayList.size(); i++){
            Log.i(WorldersBase.TAG, closeItListModelArrayList.get(i).getId() + ":::" + closeItListModelArrayList.get(i).getService());
        }
    }

    public int getCount() {
        return closeItListModelArrayList.size();
    }

    public Object getItem(int position) {
        return closeItListModelArrayList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_list_closeit, null);
            holder = new ViewHolder();
            holder.tv_id_closeit = (TextView) convertView.findViewById(R.id.tv_id_closeit);
            holder.tv_service = (TextView) convertView.findViewById(R.id.tv_service);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_id_closeit.setText(closeItListModelArrayList.get(position).getId());
        holder.tv_service.setText(closeItListModelArrayList.get(position).getService());

        return convertView;
    }

    static class ViewHolder {
        TextView tv_id_closeit, tv_service;
    }

布局

<?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:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!--  ListRow Left sied Thumbnail image -->


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_marginLeft="25dp">

        <LinearLayout
            android:id="@+id/thumbnail"
            android:layout_width="0dp"
            android:layout_weight="0.1"

            android:layout_height="wrap_content"
            android:padding="3dip"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="5dip">

            <ImageView
                android:id="@+id/list_image"
                android:layout_width="24dip"
                android:layout_height="24dip"
                android:src="@drawable/bandera_azul"/>

        </LinearLayout>

        <TextView
            android:id="@+id/message"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:lines="1"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/thumbnail"
            android:layout_toRightOf="@+id/thumbnail"
            android:text="Oferta de Pepito"
            android:textColor="@color/Blue_Background"
            android:typeface="sans"
            android:textSize="18dip"
            android:textStyle="bold"
            android:gravity="center_vertical" />

        <TextView
            android:layout_weight="0.1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/emptyCaption"
            android:id="@+id/space_between_2"
            android:layout_marginLeft="20dip" />

        <TextView
            android:id="@+id/price"
            android:layout_width="0dp"
            android:layout_weight="0.3"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/thumbnail"
            android:layout_toRightOf="@+id/thumbnail"
            android:text="3500€"
            android:textColor="@color/Blue_Background"
            android:typeface="sans"
            android:textSize="18dip"
            android:textStyle="bold"
            android:gravity="center_vertical" />
    </LinearLayout>


</RelativeLayout>

错误

05-28 13:17:00.431: E/AndroidRuntime(522): FATAL EXCEPTION: main
05-28 13:17:00.431: E/AndroidRuntime(522): android.content.res.Resources$NotFoundException: String resource ID #0xc
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.content.res.Resources.getText(Resources.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.content.res.XResources.getText(XResources.java:696)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.TextView.setText(TextView.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at com.worlders.tools.ListViewCustomCloseit.getView(ListViewCustomCloseit.java:56)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.AbsListView.obtainView(AbsListView.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.ListView.makeAndAddView(ListView.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.ListView.fillDown(ListView.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.ListView.fillFromTop(ListView.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.ListView.layoutChildren(ListView.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.AbsListView.onLayout(AbsListView.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.View.layout(View.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.ViewGroup.layout(ViewGroup.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.LinearLayout.onLayout(LinearLayout.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.View.layout(View.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.ViewGroup.layout(ViewGroup.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.LinearLayout.onLayout(LinearLayout.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.View.layout(View.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.ViewGroup.layout(ViewGroup.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:690)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.View.layout(View.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.ViewGroup.layout(ViewGroup.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.FrameLayout.onLayout(FrameLayout.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.View.layout(View.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.ViewGroup.layout(ViewGroup.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.LinearLayout.onLayout(LinearLayout.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.View.layout(View.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.ViewGroup.layout(ViewGroup.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.widget.FrameLayout.onLayout(FrameLayout.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.View.layout(View.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.ViewGroup.layout(ViewGroup.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.Choreographer.doCallbacks(Choreographer.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.Choreographer.doFrame(Choreographer.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.os.Handler.handleCallback(Handler.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.os.Handler.dispatchMessage(Handler.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.os.Looper.loop(Looper.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at android.app.ActivityThread.main(ActivityThread.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at java.lang.reflect.Method.invokeNative(Native Method)
05-28 13:17:00.431: E/AndroidRuntime(522):  at java.lang.reflect.Method.invoke(Method.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
05-28 13:17:00.431: E/AndroidRuntime(522):  at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
05-28 13:17:00.431: E/AndroidRuntime(522):  at dalvik.system.NativeStart.main(Native Method)

我不知道这是一个上下文错误还是什么。在getView中。

有什么建议吗?

4 个答案:

答案 0 :(得分:4)

更改

holder.tv_id_closeit.setText(closeItListModelArrayList.get(position).getId());

holder.tv_id_closeit.setText(String.valueOf(closeItListModelArrayList.get(position).getId()));

最有可能closeItListModelArrayList.get(position).getId()返回int,当您将其传递给setText时,Android会调用方法setText(int resID)而不是setText(CharSequence text)

所以现在Android会尝试找到与closeItListModelArrayList.get(position).getId()对应的值,当它找不到值时会抛出ResourceNotFoundException

答案 1 :(得分:3)

替换

 holder.tv_id_closeit.setText(closeItListModelArrayList.get(position).getId());

通过

 holder.tv_id_closeit.setText(""+closeItListModelArrayList.get(position).getId());

答案 2 :(得分:2)

可能这个

closeItListModelArrayList.get(position).getI‌​d()

返回一个Integer值。而且你不能直接将int值设置为textview。

使用String.valeuOf(intvalue)

holder.tv_id_closeit.setText(String.valueOf(closeItListModelArrayList.get(position).getId()););

setText确实将int作为参数,但它会查找具有上述ID的资源。如果找不到,则会获得ResourceNotFoundException

您需要的是setText,它将字符顺序作为参数。

答案 3 :(得分:0)

解决。

谢谢大家。问题是我需要将INT转换为STRING以在textview上设置。

更改

holder.tv_id_closeit.setText(closeItListModelArrayList.get(position).getId());

holder.tv_id_closeit.setText(String.valueOf{closeItListModelArrayList.get(position).getId()));