简体   繁体   中英

sqlite adapter with error android.content.res.Resources$NotFoundException: String resource ID #0xa

I faced this strange error .. what the cause of it

  Process: com.justedhak.www.i, PID: 17041
    android.content.res.Resources$NotFoundException: String resource ID #0xa
            at android.content.res.Resources.getText(Resources.java:1334)
            at android.widget.TextView.setText(TextView.java:4917)
            at com.justedhak.www.i.DBadapter.getView(DBadapter.java:60)
            at android.widget.AbsListView.obtainView(AbsListView.java:2820)
            at android.widget.GridView.onMeasure(GridView.java:1064)
            at android.view.View.measure(View.java:18557)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
            at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:607)
            at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:674)
            at android.view.View.measure(View.java:18557)

I am calling it such way

DBadapter adapter = new DBadapter(getApplicationContext(), R.layout.grid_item_layout, Objects);

this is the adapter

public class DBadapter extends ArrayAdapter<Objects> {
    private static Uri[] mUrls = null;
    private static String[] strUrls = null;
    private String[] mNames = null;
    private Cursor cc = null;
    private Context mcontext;
    private int layoutResourceId;
    private List<?> listitems;

    public DBadapter(Context context, int layoutResourceId, List<Objects> listitem) {
        super(context, layoutResourceId, listitem);
        this.layoutResourceId = layoutResourceId;
        this.mcontext = context;
        this.listitems = listitem;
        System.out.println("entering adapter");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("entering adapter1");

        View row = convertView;
        final ViewHolder holder;

        if (row == null) {
            LayoutInflater inflater = LayoutInflater.from(mcontext);
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.imageTitle = (TextView) row.findViewById(R.id.Nameview);
            holder.imageView = (ImageView) row.findViewById(R.id.imageView);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
        Objects item = getItem(position);
        System.out.println("item.getUrl() ");
        System.out.println(item.getUrl());
        Picasso.with(mcontext).setIndicatorsEnabled(true);
        holder.imageTitle.setText(item.getId());
        Picasso.
                with(mcontext).
                load(item.getUrl())
                .placeholder(R.drawable.logo)
                .fit()
                .noFade()
                .into(holder.imageView);

        holder.imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Log.d("OnImageButton", "Clicked");
                Intent intnt = new Intent(mcontext, SingleViewActivity.class);
                intnt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //Bitmap imageID=holder.imageView;
                //intnt.putExtra("ImageId", imageID);
                mcontext.startActivity(intnt);


                Toast.makeText(mcontext, "intent",
                        Toast.LENGTH_LONG).show();
            }
        });


        return row;
    }

    static class ViewHolder {
        TextView imageTitle;
        ImageView imageView;
    }
}

grid item layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:id="@+id/Nameview"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:maxLines="2"
        android:background="#ea4070"
        android:textSize="12sp" />


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="300dp"
        android:layout_height="190dp"
        android:gravity="center"
        android:layout_below="@+id/Nameview"
        />


    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:maxLines="2"
        android:layout_below="@+id/imageView"
        android:ellipsize="marquee"
        android:textSize="12sp"
        android:background="#c9c9c9"/>

</RelativeLayout>

To set Integer value into any Widget ( TextView , EditText , etc..) always convert it to String and then set.

 holder.imageTitle.setText(String.valueOf(item.getId()));

There are multiple method of setText , one takes a String and one takes an int as resource id . If you pass it an integer it will try to look for the corresponding string resource id - which it can't find, result into Resources$NotFoundException .

holder.imageTitle.setText(item.getId()+"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM