Pregunta

I have a loader class which loads pictures from assets during the start of the app.

Then I have a main menu with some buttons. When I click on one button a new layout comes up with the ViewFlipper which loads the pictures from the loader class. When I quit the ViewFlipper Activity to the main menu and start the ViewFlipper again, I'll get an Illegalstateexception.

Here is my loader class:

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));
    }
}

And here my ViewFlipper image import method:

public void loadInto(int i) {

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

frontPic,backPic, and viewInflate are ArrayLists.

I know that I have to remove all childs of the ViewFlipper before adding the new ones, but where?

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)
¿Fue útil?

Solución

Your code and the logcat output you display are out of sync.

The error says that line 392 of your psupflip.java file you call addView, inside your inflate method, and you don't.

So I guess, you moved some code before getting the logcat.

The error may only come, in the code you show, from this line :

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

Chances are that you call this code several time with the same parameter. Thus, adding the same view twice to a view group. BUT, a view can have only one parent, you can't re-add to another parent like this, you must first remove it from its first parent view group (what logcat says indeed).

Try to stay closer to java naming conventions and learn how to read logcat errors, it will help you to go further in your development.

--UPDATE

Try this to remove the view from its parent :

View v = (View) LoadingScreen.viewInflate.get(i);
v.getParent.removeView(v);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top