我在我的开发人员文档中收到此错误,我不知道为什么。我在这里用我的几部手机测试过,没有问题。
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
因为我在使用的所有东西中都调用了 super
编辑:
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>