我有一个加载器类,它在应用程序启动期间从资产加载图片。
然后我有一个带有一些按钮的主菜单。当我单击一个按钮时,ViewFlipper 会出现一个新布局,它会从加载器类加载图片。当我将 ViewFlipper Activity 退出到主菜单并再次启动 ViewFlipper 时,我会得到一个 Illegalstateexception。
这是我的加载器类:
public void inflate(){
for(int i =0;i< createArray();i++){
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bilder, null);
viewInflate.add(view);
}
Toast.makeText(getApplicationContext(), "" +viewInflate.size(), Toast.LENGTH_LONG).show();
addPics();
}
public void addPics(){
for(int i =0;i< createArray();i++){
View pic = viewInflate.get(i).findViewById(R.id.ImageView01);
View pic2 = viewInflate.get(i).findViewById(R.id.ImageView02);
((ImageView) pic).setImageBitmap((Bitmap)frontPic.get(i));
((ImageView) pic2).setImageBitmap((Bitmap)backPic.get(i));
}
}
这里是我的 ViewFlipper 图像导入方法:
public void loadInto(int i) {
flipper.addView((View) LoadingScreen.viewInflate.get(i));
}
frontPic、backPic 和 viewInflate 是 ArrayList。
我知道我必须在添加新的 ViewFlipper 之前删除所有子项,但是在哪里?
07-16 16:41:46.468: E/AndroidRuntime(26720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.psuflip}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.os.Looper.loop(Looper.java:137)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-16 16:41:46.468: E/AndroidRuntime(26720): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 16:41:46.468: E/AndroidRuntime(26720): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 16:41:46.468: E/AndroidRuntime(26720): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-16 16:41:46.468: E/AndroidRuntime(26720): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-16 16:41:46.468: E/AndroidRuntime(26720): at dalvik.system.NativeStart.main(Native Method)
07-16 16:41:46.468: E/AndroidRuntime(26720): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.view.ViewGroup.addView(ViewGroup.java:3210)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.widget.ViewAnimator.addView(ViewAnimator.java:184)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.view.ViewGroup.addView(ViewGroup.java:3155)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.view.ViewGroup.addView(ViewGroup.java:3131)
07-16 16:41:46.468: E/AndroidRuntime(26720): at com.example.myapp.psuflip.inflate(psuflip.java:392)
07-16 16:41:46.468: E/AndroidRuntime(26720): at com.example.myapp.psuflip.createFlipper(psuflip.java:374)
07-16 16:41:46.468: E/AndroidRuntime(26720): at com.example.myapp.psuflip.onCreate(psuflip.java:105)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.app.Activity.performCreate(Activity.java:5104)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-16 16:41:46.468: E/AndroidRuntime(26720): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)