带有GridView的ClassCastException

时间:2013-03-20 15:51:05

标签: android

所以我有一个使用GridView的应用程序。我正在使用支持库,因为该应用程序的目标是使用最小的API 8.所以,我写这个XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/logotipo_high_density_vinceri_movil" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView1"
    android:src="@drawable/home_high_density_vinceri_movil" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/alertas_high_density_vinceri_movil" />

<ImageView
    android:id="@+id/imagelogo"
    android:layout_width="69dp"
    android:layout_height="69dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/imageView1" />

<TextView
    android:id="@+id/detallenombrecamp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imagelogo"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/imagelogo"
    android:layout_toRightOf="@+id/imagelogo"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textocamp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/imagelogo"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/iconostaff"
    android:layout_width="78dp"
    android:layout_height="72dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textocamp"
    android:src="@drawable/icono_staff_high_density_vinceri_movil" />

<ImageView
    android:id="@+id/iconofotos"
    android:layout_width="78dp"
    android:layout_height="72dp"
    android:layout_alignTop="@+id/iconostaff"
    android:layout_toLeftOf="@+id/imageView2"
    android:src="@drawable/icono_fotos_high_density_vinceri_movil" />

<ImageView
    android:id="@+id/iconovideo"
    android:layout_width="78dp"
    android:layout_height="72dp"
    android:layout_alignTop="@+id/iconofotos"
    android:layout_toLeftOf="@+id/imageView3"
    android:src="@drawable/icono_video_high_density_vinceri_movil" />

<ImageView
    android:id="@+id/iconostats"
    android:layout_width="78dp"
    android:layout_height="72dp"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/iconovideo"
    android:src="@drawable/icono_estadistica_high_density_vinceri_movil" />

<android.support.v7.widget.GridLayout
    android:id="@+id/grid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/iconostaff" >

</android.support.v7.widget.GridLayout>

</RelativeLayout>

此活动:     import android.app.Activity;     import android.os.Bundle;     import android.widget.GridView;

public class DetalleCampActivity extends Activity {

private Integer[] imagenes={
        R.drawable.logo_carrefour,
        R.drawable.logo_mercedes,
        R.drawable.logo_venta_unica
};


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.detallecamplayout);     
    GridView grid=(GridView)findViewById(R.id.grid);//This is line 26
    grid.setAdapter(new ImageAdapter(this));

}

}

我使用这个适配器:

public class ImageAdapter extends BaseAdapter {

private Context context;
int mGalleryItemBackground;
private Integer[] imagenes={
        R.drawable.logo_carrefour,
        R.drawable.logo_mercedes,
        R.drawable.logo_venta_unica
};

public ImageAdapter(Context c) {
    context=c;
    TypedArray attr=context.obtainStyledAttributes(R.styleable.Gallery);
    mGalleryItemBackground=attr.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
    attr.recycle();
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return imagenes.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@SuppressWarnings("deprecation")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView image;
    if(convertView==null){
    image=new ImageView(context);

    image.setImageResource(imagenes[position]);
    image.setLayoutParams(new GridView.LayoutParams(150, 150));
    image.setScaleType(ImageView.ScaleType.CENTER_CROP);
    image.setPadding(8, 8, 8, 8);
    }else{
        image=(ImageView) convertView;
    }
    image.setBackgroundResource(imagenes[position]);
    return image;
}

}

它在编译时没有显示错误,但是当我运行应用程序时,我有这个堆栈跟踪:

03-20 15:31:21.498: E/AndroidRuntime(20630): FATAL EXCEPTION: main
03-20 15:31:21.498: E/AndroidRuntime(20630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.publidirecta.vincerientidad2/com.publidirecta.vincerientidad2.DetalleCampActivity}: java.lang.ClassCastException: android.support.v7.widget.GridLayout
03-20 15:31:21.498: E/AndroidRuntime(20630):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at android.os.Looper.loop(Looper.java:130)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at android.app.ActivityThread.main(ActivityThread.java:3701)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at java.lang.reflect.Method.invoke(Method.java:507)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at dalvik.system.NativeStart.main(Native Method)
03-20 15:31:21.498: E/AndroidRuntime(20630): Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout
03-20 15:31:21.498: E/AndroidRuntime(20630):    at com.publidirecta.vincerientidad2.DetalleCampActivity.onCreate(DetalleCampActivity.java:26)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-20 15:31:21.498: E/AndroidRuntime(20630):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)

在浪费了大量时间尝试不使用支持库获取ClassDefNotFoundError(并且不确定为什么它现在不抛出它)我现在有了这个。知道怎么解决吗?非常感谢你!

3 个答案:

答案 0 :(得分:4)

您正在尝试将GridLayout转换为GridView。由于显而易见,您需要GridView,因此请在XML文件中更改以下内容:

<android.support.v7.widget.GridLayout
    android:id="@+id/grid"

为:

<GridView
    android:id="@+id/grid"

或者,如果您确实需要GridLayout,请更改活动中的以下行:

GridView grid=(GridView)findViewById(R.id.grid);

为:

android.support.v7.widget.GridLayout grid = (android.support.v7.widget.GridLayout)findViewById(R.id.grid);

但是,您的适配器会出错,因为GridLayout不使用适配器。

答案 1 :(得分:0)

如果它可以解决我的问题,请在布局中以这种方式设置GridView:

<GridView
    android:id="@+id/grid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"/>

会让它看起来像一个网格。

答案 2 :(得分:0)

如果您需要严格的GridLayout,请在您的活动中进行这些调整 改变这个:

GridView grid=(GridView)findViewById(R.id.grid);

为:

android.widget.GridLayout grid = (android.widget.GridLayout)findViewById(R.id.grid);