所以我有一个使用 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 之后(并且不太确定为什么它现在不抛出它),我现在有了这个。知道如何解决吗?非常感谢你!