Java Android开发人员:ClassCastException错误

Java Android开发人员:ClassCastException错误,java,android,Java,Android,我创建了一个自定义组件类,它只是RadioGroup类,更改为扩展RelativeLayout,而不是LinearLayout。当我尝试运行我的应用程序时,我遇到以下错误: 06-16 00:01:17.573: ERROR/AndroidRuntime(567): Caused by: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative 06-16 00:01:17.573: ERROR/Androi

我创建了一个自定义组件类,它只是RadioGroup类,更改为扩展RelativeLayout,而不是LinearLayout。当我尝试运行我的应用程序时,我遇到以下错误:

06-16 00:01:17.573: ERROR/AndroidRuntime(567): Caused by: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative
06-16 00:01:17.573: ERROR/AndroidRuntime(567):     at com.stickfigs.blockball.BlockBallLevelSelect.onCreate(BlockBallLevelSelect.java:38)
第38行是指这一行:

lsRadioGroupRelative = (RadioGroupRelative) findViewById(R.id.radioGroupRelative1);
其中,该变量定义为:

private RadioGroupRelative lsRadioGroupRelative;
以下是应用程序此部分中使用的layout.xml文件:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="@drawable/background_levelselect">
    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <com.stickfigs.blockball.RadioGroupRelative
            android:id="@+id/radioGroupRelative1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="64px"
            android:layout_marginTop="96px"
            android:orientation="horizontal">
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton1"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="1">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton2"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="2"
                android:layout_marginLeft="32px"
                android:layout_toRightOf="@id/levelButton1"
                android:layout_alignTop="@id/levelButton1">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton3"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="3"
                android:layout_marginLeft="32px"
                android:layout_toRightOf="@id/levelButton2"
                android:layout_alignTop="@id/levelButton2">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton4"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="4"
                android:layout_marginTop="32px"
                android:layout_below="@id/levelButton1"
                android:layout_alignLeft="@id/levelButton1">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton5"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="5"
                android:layout_marginLeft="32px"
                android:layout_toRightOf="@id/levelButton4"
                android:layout_alignTop="@id/levelButton4">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton6"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="6"
                android:layout_marginLeft="32px"
                android:layout_toRightOf="@id/levelButton5"
                android:layout_alignTop="@id/levelButton5">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton7"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="7"
                android:layout_marginTop="32px"
                android:layout_below="@id/levelButton4"
                android:layout_alignLeft="@id/levelButton4">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton8"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="8"
                android:layout_marginLeft="32px"
                android:layout_toRightOf="@id/levelButton7"
                android:layout_alignTop="@id/levelButton7">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton9"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="9"
                android:layout_marginLeft="32px"
                android:layout_toRightOf="@id/levelButton8"
                android:layout_alignTop="@id/levelButton8">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton10"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="10"
                android:layout_marginTop="32px"
                android:layout_below="@id/levelButton7"
                android:layout_alignLeft="@id/levelButton7">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton11"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="11"
                android:layout_marginLeft="32px"
                android:layout_toRightOf="@id/levelButton10"
                android:layout_alignTop="@id/levelButton10">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton12"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="12"
                android:layout_marginLeft="32px"
                android:layout_toRightOf="@id/levelButton11"
                android:layout_alignTop="@id/levelButton11">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton13"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="13"
                android:layout_marginTop="32px"
                android:layout_below="@id/levelButton10"
                android:layout_alignLeft="@id/levelButton10">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton14"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="14"
                android:layout_marginLeft="32px"
                android:layout_toRightOf="@id/levelButton13"
                android:layout_alignTop="@id/levelButton13">
            </view>
            <view
                class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
                android:button="@drawable/bb_button"
                android:id="@+id/levelButton15"
                android:layout_height="96px"
                android:layout_width="96px"
                android:textColor="#fff"
                android:text="15"
                android:layout_marginLeft="32px"
                android:layout_toRightOf="@id/levelButton14"
                android:layout_alignTop="@id/levelButton14">
            </view>
        </com.stickfigs.blockball.RadioGroupRelative>
        <Button 
            android:id="@+id/levelButtonGo"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginLeft="128px"
            android:layout_marginTop="22px"
            android:layout_below="@id/radioGroupRelative1"
            android:text=""
            android:background="@drawable/level_select_button_play"
            android:onClick="goClicked"/>
    </RelativeLayout>
</FrameLayout>
更新更多stacktrace:

06-16 02:18:13.814: INFO/AndroidRuntime(638): NOTE: attach of thread 'Binder Thread #3' failed
06-16 02:18:13.964: INFO/ActivityManager(58): Start proc com.stickfigs.blockball for activity com.stickfigs.blockball/.BlockBallLevelSelect: pid=645 uid=10036 gids={}
06-16 02:18:14.584: DEBUG/AndroidRuntime(645): Shutting down VM
06-16 02:18:14.584: WARN/dalvikvm(645): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): FATAL EXCEPTION: main
06-16 02:18:14.654: ERROR/AndroidRuntime(645): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stickfigs.blockball/com.stickfigs.blockball.BlockBallLevelSelect}: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.os.Looper.loop(Looper.java:123)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at java.lang.reflect.Method.invokeNative(Native Method)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at java.lang.reflect.Method.invoke(Method.java:521)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at dalvik.system.NativeStart.main(Native Method)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): Caused by: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at com.stickfigs.blockball.BlockBallLevelSelect.onCreate(BlockBallLevelSelect.java:38)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     ... 11 more
06-16 02:18:14.786: WARN/ActivityManager(58):   Force finishing activity com.stickfigs.blockball/.BlockBallLevelSelect
06-16 02:18:15.326: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44ed9600 com.stickfigs.blockball/.BlockBallLevelSelect}
06-16 02:18:19.254: INFO/Process(645): Sending signal. PID: 645 SIG: 9
06-16 02:18:19.294: WARN/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44ea5ab8
06-16 02:18:19.584: INFO/ActivityManager(58): Process com.stickfigs.blockball (pid 645) has died.
06-16 02:18:20.106: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
06-16 02:18:20.783: DEBUG/dalvikvm(217): GC_EXPLICIT freed 100 objects / 4192 bytes in 125ms

尝试在中调试。设置一个断点,并检查
findViewById(R.id.radioGroupRelative1)
实际返回的内容,这将为您提供有关正在发生的事情的线索。我对调试比较陌生,如果我想这样做,我应该将断点放在哪里?在发生错误的实际行上?是的,实际在第38行设置断点。然后运行应用程序。当您到达断点时,Eclipse应该将您带入调试透视图。转到“窗口>显示视图>表达式”。输入
findviewbyd(R.id.radioGroupRelative1)
其中显示addnewexpression。然后,您可以检查对象并查看返回的内容。尝试将获取异常的代码更改为以下内容:
View temp=findViewById(R.id.radiogroup relative1);lsRadioGroupRelative=(RadioGroupRelative)温度。你仍然会得到例外(除非宇宙是一个比我想象的更奇怪的地方:-)。但是,通过在第二次赋值之前设置断点,您应该能够检查
temp
的值,并查看它是什么类型的对象。我的猜测(可能是非常错误的)是,您可能对RadioGroupRelative类有两种不同的定义。如果temp的类看起来与lsRadioGroupRelative相同,但仍然得到ClassCastException,那么一定是它。
06-16 02:18:13.814: INFO/AndroidRuntime(638): NOTE: attach of thread 'Binder Thread #3' failed
06-16 02:18:13.964: INFO/ActivityManager(58): Start proc com.stickfigs.blockball for activity com.stickfigs.blockball/.BlockBallLevelSelect: pid=645 uid=10036 gids={}
06-16 02:18:14.584: DEBUG/AndroidRuntime(645): Shutting down VM
06-16 02:18:14.584: WARN/dalvikvm(645): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): FATAL EXCEPTION: main
06-16 02:18:14.654: ERROR/AndroidRuntime(645): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stickfigs.blockball/com.stickfigs.blockball.BlockBallLevelSelect}: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.os.Looper.loop(Looper.java:123)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at java.lang.reflect.Method.invokeNative(Native Method)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at java.lang.reflect.Method.invoke(Method.java:521)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at dalvik.system.NativeStart.main(Native Method)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): Caused by: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at com.stickfigs.blockball.BlockBallLevelSelect.onCreate(BlockBallLevelSelect.java:38)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-16 02:18:14.654: ERROR/AndroidRuntime(645):     ... 11 more
06-16 02:18:14.786: WARN/ActivityManager(58):   Force finishing activity com.stickfigs.blockball/.BlockBallLevelSelect
06-16 02:18:15.326: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44ed9600 com.stickfigs.blockball/.BlockBallLevelSelect}
06-16 02:18:19.254: INFO/Process(645): Sending signal. PID: 645 SIG: 9
06-16 02:18:19.294: WARN/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44ea5ab8
06-16 02:18:19.584: INFO/ActivityManager(58): Process com.stickfigs.blockball (pid 645) has died.
06-16 02:18:20.106: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
06-16 02:18:20.783: DEBUG/dalvikvm(217): GC_EXPLICIT freed 100 objects / 4192 bytes in 125ms