简体   繁体   中英

Class Cast Exception Issue

Seeing strange behavior in my android application. I have a meter and a image view in my android xml. It was working fine and just on changing the position of the widgets in my xml graphical view am getting this ClassCastException. Following is my XML file.

<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:background="@drawable/halo"
    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=".hello" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="perform" />

    <org.codeandmagic.android.gauge.GaugeView
        android:id="@+id/gauge_view1"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_weight="1" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_alignLeft="@+id/gauge_view1"
        android:layout_centerVertical="true"
        android:src="@drawable/images" />

</RelativeLayout>

The logcat shows me the following on application crashing:

09-18 11:02:01.411: E/AndroidRuntime(21312): FATAL EXCEPTION: main
09-18 11:02:01.411: E/AndroidRuntime(21312): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hello.hello}: java.lang.ClassCastException: android.widget.Button cannot be cast to org.codeandmagic.android.gauge.GaugeView
09-18 11:02:01.411: E/AndroidRuntime(21312):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
09-18 11:02:01.411: E/AndroidRuntime(21312):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
09-18 11:02:01.411: E/AndroidRuntime(21312):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
09-18 11:02:01.411: E/AndroidRuntime(21312):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
09-18 11:02:01.411: E/AndroidRuntime(21312):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 11:02:01.411: E/AndroidRuntime(21312):    at android.os.Looper.loop(Looper.java:154)
09-18 11:02:01.411: E/AndroidRuntime(21312):    at android.app.ActivityThread.main(ActivityThread.java:4945)
09-18 11:02:01.411: E/AndroidRuntime(21312):    at java.lang.reflect.Method.invokeNative(Native Method)
09-18 11:02:01.411: E/AndroidRuntime(21312):    at java.lang.reflect.Method.invoke(Method.java:511)
09-18 11:02:01.411: E/AndroidRuntime(21312):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-18 11:02:01.411: E/AndroidRuntime(21312):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-18 11:02:01.411: E/AndroidRuntime(21312):    at dalvik.system.NativeStart.main(Native Method)
09-18 11:02:01.411: E/AndroidRuntime(21312): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to org.codeandmagic.android.gauge.GaugeView

Can anybody please explain why is it happening in my case? and then the biggest question what's the position of widgets has to do with it??

While initialise "org.codeandmagic.android.gauge.GaugeView" custom class's object , there is problem with casting. otherwise your xml is correct if there cutom class is exist with correct package name.

check out initialisation or please add that code at here

Do a project cleanup. It's a common issue.

Eclipse - Project - Clean - Select your project - Ok

I'm not sure why this happens, but I guess that some times, when you move a view, it gets moved on the XML but not on the compilated code. With a project cleanup, the compilated code is regenerated and then, correctly placed.

It's a common issues. clean a project.and check the variable type cast in java file.

like example

Button b=(Button)findViewById(R.id.button1);

or

GaugeView gv=(GaugeView)findViewById(R.id.gauge_view1);

May be solve the problm.

You should check where you define your widgets like

Button btn1=(Button)findViewById(R.id.button1);
GaugeView gv=(GaugeView)findViewById(R.id.gauge_view1);
Imageview iv=(ImageView)findViewById(R.id.imageView1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM