GridView故障classCastException

时间:2015-03-16 20:58:13

标签: android xml android-layout gridview

我一直在讨论这个...我的代码似乎没有用,我找不到错误。
我确信发生错误是因为我在XML中格式错误。你们中的一些人可以看看吗。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="gokerakinci.lottery.Sayisal" >

<LinearLayout
    android:id="@+id/llNumbers"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:orientation="horizontal"
    android:weightSum="6" >

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="2dp"
        android:text="" />

    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="2dp"
        android:text="" />

    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="2dp"
        android:text="" />

    <Button
        android:id="@+id/b4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="2dp"
        android:text="" />

    <Button
        android:id="@+id/b5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="2dp"
        android:text="" />

    <Button
        android:id="@+id/b6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="2dp"
        android:text="" />
</LinearLayout>

<View
    android:id="@+id/divider"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@id/llNumbers"
    android:layout_marginBottom="4dp"
    android:layout_marginTop="4dp"
    android:background="@android:color/darker_gray" />

<RelativeLayout
    android:id="@+id/rlDown"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" 
    >

    <Button
        android:id="@+id/bAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:padding="8dp"
        android:text="@string/add_new_row" />

    <Button
        android:id="@+id/bDone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/bAdd"
        android:text="@string/done" />

    <Button
        android:id="@+id/bDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/bAdd"
        android:text="@string/delete" />
</RelativeLayout>

<View
    android:id="@+id/divider1"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_above="@id/rlDown"
    android:layout_marginBottom="4dp"
    android:layout_marginTop="4dp"
    android:background="@android:color/darker_gray" />

<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/divider1"
    android:layout_below="@id/divider"
    android:clickable="true"
    android:columnWidth="90dp"
    android:focusable="true"
    android:gravity="center"
    android:horizontalSpacing="4dp"
    android:numColumns="5"
    android:paddingTop="8dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="8dp" />

</RelativeLayout>

这是Logcat输出:

03-16 21:54:03.131: E/AndroidRuntime(27952): FATAL EXCEPTION: main
03-16 21:54:03.131: E/AndroidRuntime(27952): Process: gokerakinci.lottery,           PID: 27952
03-16 21:54:03.131: E/AndroidRuntime(27952): java.lang.RuntimeException: Unable to start activity ComponentInfo{gokerakinci.lottery/gokerakinci.lottery.Sayisal}:    java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.GridView
03-16 21:54:03.131: E/AndroidRuntime(27952):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at android.app.ActivityThread.access$800(ActivityThread.java:156)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at android.os.Looper.loop(Looper.java:157) 
03-16 21:54:03.131: E/AndroidRuntime(27952):    at android.app.ActivityThread.main(ActivityThread.java:5872)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at java.lang.reflect.Method.invokeNative(Native Method)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at java.lang.reflect.Method.invoke(Method.java:515)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at dalvik.system.NativeStart.main(Native Method)
03-16 21:54:03.131: E/AndroidRuntime(27952): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.GridView
03-16 21:54:03.131: E/AndroidRuntime(27952):    at gokerakinci.lottery.Sayisal.setInterface(Sayisal.java:35)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at gokerakinci.lottery.Sayisal.onCreate(Sayisal.java:29)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at android.app.Activity.performCreate(Activity.java:5312)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
03-16 21:54:03.131: E/AndroidRuntime(27952):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
03-16 21:54:03.131: E/AndroidRuntime(27952):    ... 11 more

这是第35行,我显然是问题所在,但在我用XML更改了一些内容之前工作正常:

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

0 个答案:

没有答案