我通过
删除了ViewSwitcher中的所有子视图mSwitcher.removeAllViews();
但是当我添加一个先前删除的视图时,它会抛出一个java.lang.IllegalStateException并说我没有从它的父视图中删除这个视图。
以下是代码:
View curView = mSwitcher.getCurrentView();
mSwitcher.removeAllViews();
Log.d(TAG, "parent >> " + nextView.getParent());
Log.d(TAG, "switcher >> " + mSwitcher);
mSwitcher.addView(curView);
mSwitcher.addView(nextView);
mSwitcher.showNext();
异常抛出了这一行
mSwitcher.addView(nextView);
这是日志:
09-03 10:04:14.551 D/MainActivity( 8852): parent >> null
09-03 10:04:14.551 D/MainActivity( 8852): switcher >> android.widget.ViewSwitcher@43ebb580
09-03 10:04:14.551 D/AndroidRuntime( 8852): Shutting down VM
09-03 10:04:14.551 W/dalvikvm( 8852): threadid=1: thread exiting with uncaught exception (group=0x4001d7e8)
09-03 10:04:14.571 E/AndroidRuntime( 8852): FATAL EXCEPTION: main
09-03 10:04:14.571 E/AndroidRuntime( 8852): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.view.ViewGroup.addView(ViewGroup.java:1865)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.widget.ViewAnimator.addView(ViewAnimator.java:160)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.widget.ViewSwitcher.addView(ViewSwitcher.java:66)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.view.ViewGroup.addView(ViewGroup.java:1822)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.view.ViewGroup.addView(ViewGroup.java:1802)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at com.aimi.appstore.tulipa.ui.activity.MainActivity.setSelected(MainActivity.java:104)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at com.aimi.appstore.tulipa.ui.activity.MainActivity$2.onClick(MainActivity.java:54)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.view.View.performClick(View.java:2408)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.view.View$PerformClick.run(View.java:8818)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.os.Handler.handleCallback(Handler.java:587)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.os.Looper.loop(Looper.java:123)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at java.lang.reflect.Method.invoke(Method.java:521)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-03 10:04:14.571 E/AndroidRuntime( 8852): at dalvik.system.NativeStart.main(Native Method)