我正在尝试在gridView上显示图像,但却出现以下错误: -
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ImageView
at com.hubdatasolutions.receiptcloud.ReceiptTabActivity$ReceiptAdapter.getView(ReceiptTabActivity.java:128)
row_grid.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:contentDescription="clickedImage"
android:src="#ffffff" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_alignParentRight="true"
android:src="@drawable/close"
/>
</RelativeLayout>
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
activity_receipt_tab.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/menu_bg"
tools:context=".ReceiptTabActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/logo" />
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
<include layout="@layout/tab_footer" />
</LinearLayout>
</RelativeLayout>
ReceptTabActivity.java
这是java文件,我收到错误。这是我试图开发两个类的文件。我在同一个文件中膨胀gridView。
public class ReceiptTabActivity extends BaseTabActivity {
GridView mGrid;
ReceiptAdapter mAdapter;
ArrayList<ReceiptData> mReceiptList = new ArrayList<ReceiptData>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receipt_tab);
mReceiptList = APICaller.getInstance().getReceiptList();
mGrid = (GridView) findViewById(R.id.gridView);
// mAdapter = new ReceiptAdapter(this, R.layout.receipt_grid_item);
//mGrid.setAdapter(mAdapter);
mAdapter = new ReceiptAdapter(this,R.layout.row_grid);
mGrid.setAdapter(mAdapter);
APICaller.getInstance().downloadPicData();
}
@Override
public void onRefreshData(Refreshable refreshable, int requestCode) {
if (requestCode == APICaller.DOWNLOAD_PIC_REQUEST_CODE) {
for (ReceiptData receipt : mReceiptList) {
String filePath = receipt.getFilePath();
if (filePath != null && !filePath.isEmpty()) {
File file = new File(filePath);
Bitmap bitmap = decodeFile(file);
if (bitmap != null)
receipt.setImage(bitmap);
else
Log.i("Bitmap", "Still null");
} else {
Log.i("String", "Still null");
}
}
mAdapter.notifyDataSetChanged();
}
}
// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE
&& o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
private Bitmap decodeOriginalFile(File f) {
try {
return BitmapFactory.decodeStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
}
return null;
}
class ReceiptAdapter extends ArrayAdapter<View> {
Context mContext;
int mResource;
ArrayList<ReceiptData> data = new ArrayList<ReceiptData>();
public ReceiptAdapter(Context context, int resource) {
super(context, resource);
mContext = context;
mResource = resource;
}
@Override
public int getCount() {
return mReceiptList.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//ImageButton mainView;
ImageView mainView;
//View row = convertView;
//RecordHolder holder = null;
mainView = (ImageView) findViewById(R.id.item_image);
//String info = ImageView.getText().toString();
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
mainView = (ImageView) inflater.inflate(mResource, parent, false);
Bitmap bitmap = mReceiptList.get(position).getImage();
if (bitmap != null)
mainView.setImageBitmap(bitmap);
return mainView;
}
}
}
答案 0 :(得分:1)
在适配器类的构造函数中初始化inflater
LayoutInaflater inflater;
public ReceiptAdapter(Context context, int resource) {
super(context, resource);
mContext = context;
mResource = resource;
infalter= LayoutInflater.from(context);
}
将getView
更改为
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null)
{
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.row_grid, null);
// inflate row_grid.xml
viewHolder.mainView= (ImageView)convertView.findViewById(R.id.item_image);
//initialize imageview
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolderItem) convertView.getTag();
}
Bitmap bitmap = mReceiptList.get(position).getImage();
if (bitmap != null)
holder.mainView.setImageBitmap(bitmap);
// set bitmap to imageview
return convertView;
}
使用ViewHolder
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
static ViewHolder
{
ImageView mainView;
}
答案 1 :(得分:0)
对于这两行:
mainView = (ImageView) findViewById(R.id.item_image);
...
mainView = (ImageView) inflater.inflate(mResource, parent, false);
似乎你做了一个复制粘贴错误。是的,你希望第一个被转换为ImageView
,但我认为第二个mainView
被夸大应该是你在那里写的第一个xml片段中的LinearLayout
?
答案 2 :(得分:0)
在你的适配器的getView中,首先给row_grid充气,然后用它来寻找你的ImageView
View rowview = (View) inflater.inflate(mResource, parent, false);
mainView = (ImageView) rowview.findViewById(R.id.item_image);
进行所需的任何自定义,然后返回row_grid(上面代码中的rowview)