Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有GridView的ClassCastException_Android - Fatal编程技术网

Android 带有GridView的ClassCastException

Android 带有GridView的ClassCastException,android,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="m

所以我有一个使用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>
我使用这个适配器:

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;
}

}
编译时没有显示错误,但当我运行应用程序时,我有以下stacktrace:

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并且不确定为什么它现在不抛出它之后,我现在有了这个。知道怎么修吗?多谢各位

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

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


但是,由于GridLayout不使用适配器,因此适配器将出现错误。

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

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


但是,由于GridLayout不使用适配器,因此您的适配器将出现错误。

如果它有助于解决我的问题,请在布局中以这种方式设置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"/>

将使其看起来像一个网格。

如果它有助于解决我的问题,请在布局中以这种方式设置网格视图:

<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"/>

将使其看起来像网格。

如果您非常需要网格布局,请在活动中进行这些调整 更改此项:

GridView grid=GridViewfindViewByIdR.id.grid

致:


android.widget.GridLayout grid=android.widget.gridlayoutfindv iewbydr.id.grid

如果您非常需要GridLayout,请在活动中进行这些调整 更改此项:

GridView grid=GridViewfindViewByIdR.id.grid

致:


android.widget.GridLayout grid=android.widget.gridlayoutfindv iewbydr.id.grid

您能给我们您的活动进口声明吗?已编辑。当我编辑的时候,我想我找到了。。。我正在导入普通的GridView类,而不是支持库类。我说得对吗?没错。看看下面我的答案。我真是太蠢了…:您能给我们您的活动进口声明吗?已编辑。当我编辑的时候,我想我找到了。。。我正在导入普通的GridView类,而不是支持库类。我说得对吗?没错。看看下面我的答案。我真是太蠢了…:这是因为网格布局不使用适配器,就像线性布局不使用适配器一样。看到了吗?但是我需要一个GridView,这样我就可以在网格中显示图像了。如果我没有错的话,我需要一个GridView。所以使用GridView。它从API级别1开始提供,因此您根本不需要支持库和GridLayout。或者,如果您的图像是有限集,请使用GridLayout,但手动将您的子对象放入其中。不,我需要支持库来使用GridView。如果我在没有支持库的情况下尝试使用它,Eclipse会告诉我它需要它。他们是不同的。对于网格视图,您不需要支持库。看到这个-还有这个-这是因为GridLayouts不使用适配器,就像LinearLayouts不使用适配器一样。看到了吗?但是我需要一个GridView,这样我就可以在网格中显示图像了。如果我没有错的话,我需要一个GridView。所以使用GridView。它从API级别1开始提供,因此您根本不需要支持库和GridLayout。或者,如果您的图像是有限集,请使用GridLayout,但手动将您的子对象放入其中。不,我需要支持库来使用GridView。如果我在没有支持库的情况下尝试使用它,Eclipse会告诉我它需要它。他们是不同的。对于网格视图,您不需要支持库。看这个,还有这个-
android.support.v7.widget.GridLayout grid = (android.support.v7.widget.GridLayout)findViewById(R.id.grid);
<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"/>