1

所以我有一个使用 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>

这个Activity:导入android.app.Activity;导入android.os.Bundle;导入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 之后(并且不太确定为什么它现在不抛出它),我现在有了这个。知道如何解决吗?非常感谢你!

4

3 回答 3

4

您正在尝试将 to 投射GridLayoutGridView. 由于很明显您需要一个GridView,因此在您的 XML 文件中更改以下内容:

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

到:

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

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

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

到:

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

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

于 2013-03-20T15:59:11.550 回答
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"/>

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

于 2013-03-20T16:25:17.620 回答
0

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

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

到:

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

于 2017-05-10T06:18:58.640 回答