您好我已经开发了一个带有图像的网格视图,我已经为它编写了一个适配器,它工作得很好,因为网格只有一个图像,现在我试图将文本放在图像下方的网格中,从那以后它就崩溃了,这是崩溃日志
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
at android.widget.LinearLayout.measureVertical(LinearLayout.java:655)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15479)
at android.widget.GridView.onMeasure(GridView.java:1044)
at android.view.View.measure(View.java:15479)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
at android.view.View.measure(View.java:15479)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4828)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15479)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15479)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4828)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2359)
at android.view.View.measure(View.java:15479)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1968)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1214)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1387)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
这是我的自定义适配器类
public class ImageAdapter extends BaseAdapter
{
private final String[] subText = {};
Context mContext;
public ImageAdapter(Context _context, String[] subText)
{
super();
mContext = _context;
}
public static String[] mThumbIds ={};
public static String[] mVideoIds = {};
public static final String[] SubText = new String[] {
"text1", "text2","text3", "text4","text1", "text2","text3", "text4" ,"text1", "text2","text3", "text4" ,"text1", "text2","text3", "text4" };
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Add The Image!!!
View gridView;
LayoutInflater inflater = (LayoutInflater)mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = new View(mContext);
gridView = inflater.inflate(R.layout.grid_item,null);
//add Text
TextView textView = (TextView)gridView.findViewById(R.id.grid_item_text);
textView.setText(SubText[position]);
ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
imageView.setImageURI(Uri.parse(mThumbIds[position]));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
return gridView;
}
}
任何人都可以告诉我我哪里出错了。
答案 0 :(得分:1)
问题很明显:您正在尝试将android.widget.AbsListView $ LayoutParams转换为android.widget.LinearLayout $ LayoutParams。
改变这个:
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
有了这个:
imageView.setLayoutParams(new TheLayoutYouUseOnGrid_item.LayoutParams(150, 150));
我看到您正在为网格的每个项目夸大自定义布局 R.layout.grid_item 。因此,当您在ImageView上更改params时,必须使用包含ImageView的LayoutParams。
另外我认为你没有很好地编写getView方法。如果可以,请发布grid_item布局。
答案 1 :(得分:1)
这一行
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
应改为
gridView.setLayoutParams(new GridView.LayoutParams(150, 150));
错误是因为当GridView期待LinearLayout
布局参数时,您的自定义膨胀视图有GridView
参数