gpt4 book ai didi

java - android.widget.LinearLayout 无法转换为 android.widget.ImageView?

转载 作者:行者123 更新时间:2023-11-30 03:14:31 26 4
gpt4 key购买 nike

我试图在 gridView 上显示图像,但出现以下错误:-

 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ImageView
at com.hubdatasolutions.receiptcloud.ReceiptTabActivity$ReceiptAdapter.getView(ReceiptTabActivity.java:128)

行网格.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;
}

}
}

最佳答案

在适配器类的构造函数中初始化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;
}

关于java - android.widget.LinearLayout 无法转换为 android.widget.ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20360570/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com