0

我有一个加载器类,它在应用程序启动期间从资产加载图片。

然后我有一个带有一些按钮的主菜单。当我单击一个按钮时,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)
4

1 回答 1

1

您的代码和您显示的 logcat 输出不同步。

该错误表明您在 inflate 方法中调用 addView 的 psupflip.java 文件的第 392 行,而您没有。

所以我猜,你在获取 logcat 之前移动了一些代码。

错误可能只会出现在您显示的代码中,来自这一行:

 flipper.addView((View) LoadingScreen.viewInflate.get(i));       

您可能会使用相同的参数多次调用此代码。因此,将同一视图两次添加到视图组。但是,一个视图只能有一个父级,您不能像这样重新添加到另一个父级,您必须首先将其从其第一个父级视图组中删除(logcat 确实如此)。

尽量靠近java 命名约定,学习如何阅读 logcat 错误,它将帮助您在开发中走得更远。

- 更新

试试这个从它的父视图中删除视图:

View v = (View) LoadingScreen.viewInflate.get(i);
v.getParent.removeView(v);
于 2013-07-16T16:46:43.777 回答