设置动画时出现ClassCastException

时间:2012-05-24 13:41:25

标签: android android-animation classcastexception

我在开发人员文档中收到此错误,但我不确定原因。我在这里的一些手机上测试了它并没有问题。

private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;

              private void swipeInit() {

                    viewFlipper = (ecm2.android.ContentViewFlipper) findViewById(R.id.statusFlipper);
                    slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
// line 2366        slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out); //error
                    slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
                    slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
}

记录错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{ecm2.android/ecm2.android.MainActivity}: java.lang.ClassCastException: android.widget.ViewFlipper
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1833)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1854)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1041)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.ViewFlipper
at ecm2.android.MainActivity.swipeInit(MainActivity.java:2366)
at ecm2.android.MainActivity.onCreate(MainActivity.java:837)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1797)

日志显示它在第2366行设置其中一个动画,但在此之前完成了一个,所以我不确定这里的问题

动画片xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="300"/>

这一切都是在我切换到自定义viewflipper以解决android错误时开始的。我真的没有做任何事情,除了试图在onDetachedFromWindow中捕捉一个异常它会完成它通常会做的一切,因为我在我使用的所有东西中调用超级

编辑:

ContentViewFlipper类

public class ContentViewFlipper extends ViewFlipper {
//class is used to try to prevent force closes on certain phone where the onDetachFromWindow would be 
//called when its not suppose to, this is an android bug

public ContentViewFlipper(Context context) {
    super(context);
}

public ContentViewFlipper( Context context, AttributeSet attrs ) {
      super( context, attrs );
   }

@Override
protected void onDetachedFromWindow() {
      try {
         super.onDetachedFromWindow();
      }
      catch( Exception e ) {
          stopFlipping();
      }
   }
}

viewFlipper声明

private ContentViewFlipper viewFlipper;

XML

<ecm2.android.ContentViewFlipper
    android:id="@+id/statusFlipper"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/incLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="40dp"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/lvIncidents"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:divider="@drawable/divider"
            android:dividerHeight="2dip"
            android:visibility="visible" >
        </ListView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/statusLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" android:layout_marginBottom="40dp">

        <ListView
            android:id="@+id/lvStatus"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:choiceMode="singleChoice"
            android:divider="@drawable/divider"
            android:dividerHeight="2dip"
            android:visibility="visible" android:layout_marginTop="50dp">
        </ListView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/dlLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" android:layout_marginBottom="40dp">

        <ListView
            android:id="@+id/lvDistList"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="@drawable/divider"
            android:dividerHeight="2dip"
            android:visibility="visible" android:layout_marginTop="50dp">
        </ListView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/emNotesLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" android:layout_marginBottom="40dp">

        <ListView
            android:id="@+id/lvEmNotes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" android:layout_marginTop="50dp">
        </ListView>

    </LinearLayout>


    <LinearLayout
        android:id="@+id/stCalenLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" android:layout_marginBottom="40dp">

        <ListView
            android:id="@+id/lvStCalendar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" android:layout_marginTop="50dp">
        </ListView>

    </LinearLayout>

</ecm2.android.ContentViewFlipper>

2 个答案:

答案 0 :(得分:0)

我猜可能会对行号产生一些混淆,并且您的对象viewFlipper被定义为ViewFlipper viewFlipper,当您从ecm2.android.ContentViewFlipper转换它时可能会出现问题,如果这不是一个后代。

答案 1 :(得分:0)

由于异常是说错误发生了android.widget.ViewFlipper,我很确定viewFlipper = ...行的任何一行都有错误的类型。

viewFlipper变量是android.widget.ViewFlipper,布局中的控件是ecm2.android.ContentViewFlipper,无法转换为android.widget.ViewFlipper,或布局中的控件是android.widget.ViewFlipper,无法转换为ecm2.android.ContentViewFlipper

在将断点设置为viewFlipper = ...行后,在调试模式下单步执行代码时,哪一行确实发生了错误?