0

这是我拥有的 XML:

<ImageView
    android:id="@+id/ivRockPic"
    android:layout_width="230dp"
    android:layout_height="230dp"
    android:layout_gravity="center"
    android:background="@drawable/image_border" />

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="100" >

        <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="50"
                android:weightSum="100"
                android:id="@+id/llAns1"
                android:orientation="horizontal">

            <TextView
                android:id="@+id/tvAns1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="50"
                android:text="Ans1"
                android:textSize="20sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/ivAns1"
                android:layout_gravity="right"
                android:layout_weight="50"
                android:layout_width="100dp"
                android:layout_height="50dp"
                />

        </LinearLayout>

        <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="50"
                android:weightSum="100"
                android:id="@+id/llAns2"
                android:orientation="horizontal">

            <TextView
                android:id="@+id/tvAns2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Ans2"
                android:layout_weight="50"
                android:textSize="20sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/ivAns2"
                android:layout_gravity="right"
                android:layout_width="100dp"
                android:layout_weight="50"
                android:layout_height="50dp"
                 />
        </LinearLayout>
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="100" >

        <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="50"
                android:weightSum="100"
                android:id="@+id/llAns3"
                android:orientation="horizontal">

            <TextView
                android:id="@+id/tvAns3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="50"
                android:text="Ans3"
                android:textSize="20sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/ivAns3"
                android:layout_gravity="right"
                android:layout_width="100dp"
                android:layout_weight="50"
                android:layout_height="50dp"
                />
        </LinearLayout>

        <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="50"
                android:weightSum="100"
                android:id="@+id/llAns4"
                android:orientation="horizontal">

            <TextView
                android:id="@+id/tvAns4"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="50"
                android:text="Ans4"
                android:textSize="20sp"
                android:textStyle="bold" />
            <ImageView
                android:id="@+id/ivAns4"
                android:layout_gravity="right"
                android:layout_width="100dp"
                android:layout_weight="50"
                android:layout_height="50dp"
                 />
        </LinearLayout>
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="100" >

        <TextView
            android:id="@+id/tvAnswer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="50" />

        <Button
            android:id="@+id/bNext"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="50"
            android:text="Next" />
    </TableRow>
</TableLayout>

只贴了一个按钮。这重复了。这个设置工作得很好。经过测试,在 bean 中初始化,它正在工作。但这是一个问题,当我将 ImageView 移到 TextView 上方时,它会在我获取 ImageView id 的视图的启动行上引发错误。但是当我把它移回来时,它工作正常。有任何想法吗?

错误:

里面的两个方法只是字符串的初始化:

res = getResources();
        img = (ImageView)findViewById(R.id.ivRockPic);
        tvAnswer = (TextView) findViewById(R.id.tvAnswer);
        initializeRussian();
        initAnswerList();
        next = (Button)findViewById(R.id.bNext);
        iViews[0] = (ImageView) findViewById(R.id.ivAns1);//dies
        iViews[1] = (ImageView) findViewById(R.id.ivAns2);
        iViews[2] = (ImageView) findViewById(R.id.ivAns3);
        iViews[3] = (ImageView) findViewById(R.id.ivAns4);
        lLayouts[0] = (LinearLayout) findViewById(R.id.llAns1);
        lLayouts[1] = (LinearLayout) findViewById(R.id.llAns2);
        lLayouts[2] = (LinearLayout) findViewById(R.id.llAns3);
        lLayouts[3] = (LinearLayout) findViewById(R.id.llAns4);
        tViews[0] = (TextView) findViewById(R.id.tvAns1);
        tViews[1] = (TextView) findViewById(R.id.tvAns2);
        tViews[2] = (TextView) findViewById(R.id.tvAns3);
        tViews[3] = (TextView) findViewById(R.id.tvAns4);

06-29 23:57:09.838: E/AndroidRuntime(5236): Uncaught handler: thread main exiting due to uncaught exception
06-29 23:57:09.869: E/AndroidRuntime(5236): java.lang.RuntimeException: Unable to start activity ComponentInfo{rock.school.rzn.rockquiz/rock.school.rzn.rockquiz.GameScreen}: java.lang.ClassCastException: android.widget.TextView
06-29 23:57:09.869: E/AndroidRuntime(5236):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at android.os.Looper.loop(Looper.java:123)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at java.lang.reflect.Method.invoke(Method.java:521)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at dalvik.system.NativeStart.main(Native Method)
06-29 23:57:09.869: E/AndroidRuntime(5236): Caused by: java.lang.ClassCastException: android.widget.TextView
06-29 23:57:09.869: E/AndroidRuntime(5236):     at rock.school.rzn.rockquiz.GameScreen.initialize(GameScreen.java:171)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at rock.school.rzn.rockquiz.GameScreen.onCreate(GameScreen.java:47)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-29 23:57:09.869: E/AndroidRuntime(5236):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-29 23:57:09.869: E/AndroidRuntime(5236):     ... 11 more
4

3 回答 3

0

据我从日志中可以看出,错误是您正在尝试转换ImageViewtoTextView或反之亦然,问题initialize()出在GameScreen类的方法和第171行..

看这里:

Caused by: java.lang.ClassCastException: android.widget.TextView
06-29 23:57:09.869: E/AndroidRuntime(5236):     at rock.school.rzn.rockquiz.GameScreen.initialize(GameScreen.java:171)
于 2013-06-29T20:12:07.653 回答
0

ClassCastException意味着您正在尝试将某些内容强制转换为不兼容的类型。在你的情况下,这条线

iViews[0] = (ImageView) findViewById(R.id.ivAns1);

检索带有 id 的东西R.id.ivAns1并尝试将其转换为ImageView. 从异常消息来看,这个 id 的视图实际上是 a TextView,这就是你的演员失败的原因。

ivAns1尽管XML 中带有 id 的视图是ImageView,但我会仔细检查文件的其余部分以确保没有其他元素具有相同的 id。也尝试重新创建你的 R 文件。

于 2013-06-29T20:12:07.903 回答
0

好吧,这似乎是一个奇怪的错误。我尝试重新定位、重建、添加新组件,最后,我只是用不同的名称重新制作了文件,它就可以工作了。我认为这是文件或 Eclipse 路径之类的问题。

于 2013-07-10T20:52:07.643 回答