我有一个片段MobilesFragment,我想在其中显示一个gridview,它将包含两个textview和一个imageview,但在我第一次运行时,th是一个正在发生的异常,
E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x64
at android.content.res.Resources.getText(Resources.java:201)
at android.widget.TextView.setText(TextView.java:2863)
at com.devs.fookart.GridViewAdapter.getView(GridViewAdapter.java:54)
at android.widget.AbsListView.obtainView(AbsListView.java:1430)
at android.widget.GridView.onMeasure(GridView.java:935)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
at android.view.View.measure(View.java:8313)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8313)
at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
我的gridview布局文件:
<?xml version="1.0" encoding="utf-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dataGrid"
android:numColumns="2"
android:layout_gravity="center_horizontal" >
</GridView>
这是我的Fragment类:
package com.ui.fragments;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import com.data.dataclass.GridViewData;
import com.devs.fookart.GridViewAdapter;
import com.devs.fookart.R;
import java.util.ArrayList;
public class MobilesFragment extends Fragment
{ private GridViewAdapter adapter;
private GridView mList;
private Context context;
private ArrayList<GridViewData> gridItems;
private String titles[]={"Headphones","Camera","Home","Mobile"};
private int prices[]={100,122,212,122};
private ArrayList<Bitmap> bmp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{ View rootView=inflater.inflate(R.layout.fragment_mobiles,container,false);
context=container.getContext();
bmp=new ArrayList<Bitmap>();
bmp.add(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_headphones));
bmp.add(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_camera));
bmp.add(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_home));
bmp.add(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_mobile));
gridItems=new ArrayList<GridViewData>();
mList=(GridView)rootView.findViewById(R.id.dataGrid);
gridItems.add(new GridViewData(1,titles[0],bmp.get(2),prices[0]));
gridItems.add(new GridViewData(2,titles[1],bmp.get(2),prices[1]));
gridItems.add(new GridViewData(3,titles[2],bmp.get(3),prices[2]));
gridItems.add(new GridViewData(4,titles[3],bmp.get(2),prices[3]));
adapter=new GridViewAdapter(context,gridItems);
mList.setAdapter(adapter);
return rootView;
}
}
GridItem布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
android:id="@+id/title"
android:layout_weight="0.02" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/product"
android:layout_weight="0.36"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Price"
android:id="@+id/price"
android:layout_weight="0.02" />
</LinearLayout>
GridViewData类:
package com.data.dataclass;
import android.graphics.Bitmap;
public class GridViewData
{ private String name;
private int price,id;
private Bitmap image;
public GridViewData(int id,String name,Bitmap image,int price)
{ this.name=name;
this.image=image;
this.price=price;
this.id=id;
}
public int getId()
{ return id;
}
public void setId(int id)
{ this.id = id;
}
public String getName()
{ return name;
}
public void setName(String name)
{ this.name=name;
}
public int getPrice()
{ return price;
}
public void setPrice(int price)
{ this.price=price;
}
public Bitmap getImage()
{ return image;
}
public void setImage(Bitmap image)
{ this.image=image;
}
}
最后一个GridView Adapter类:
package com.devs.fookart;
import android.app.Activity;
import android.content.Context;
import android.widget.TextView;
import android.widget.ImageView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.data.dataclass.GridViewData;
import java.util.ArrayList;
public class GridViewAdapter extends BaseAdapter
{ private ArrayList<GridViewData> GridItem;
private Context context;
public GridViewAdapter(Context context,ArrayList<GridViewData> GridItem)
{ this.context=context;
this.GridItem=GridItem;
}
@Override
public int getCount()
{ return GridItem.size();
}
@Override
public Object getItem(int position)
{ return GridItem.get(position);
}
@Override
public long getItemId(int position)
{ return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{ if(convertView==null)
{ LayoutInflater mInflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView=mInflater.inflate(R.layout.product_grid,null);;
}
ImageView img=(ImageView)convertView.findViewById(R.id.product);
TextView title=(TextView)convertView.findViewById(R.id.title);
TextView price=(TextView)convertView.findViewById(R.id.price);
img.setImageBitmap(GridItem.get(position).getImage());
title.setText(GridItem.get(position).getName());
price.setText(GridItem.get(position).getPrice());
return convertView;
}
}
这将是一个巨大的帮助,非常感谢提前......
答案 0 :(得分:0)
看起来getPrice()
正在返回int
并且您将它传递给setText(),这可能会导致此问题,因为此整数被解释为{{1}中某些字符串的资源标识符像strings.xml
一样。
改变这个:
R.string.hello_world
到此:
price.setText(GridItem.get(position).getPrice());
请告诉我这是否有帮助:)