MrGibbage
MrGibbage

Reputation: 2646

The specified child already has a parent in google alarmclock code

I am modifying some google code for my current project. I just started getting an error in this class, which is puzzling because I haven't made any changes to this class at all.

Here's the error:

 FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skipmorrow.powerclock/com.skipmorrow.powerclock.SetAlarm}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
    at android.app.ActivityThread.access$600(ActivityThread.java:150)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5191)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
    at android.view.ViewGroup.addView(ViewGroup.java:3210)
    at android.view.ViewGroup.addView(ViewGroup.java:3186)
    at com.skipmorrow.powerclock.SetAlarm.onCreate(SetAlarm.java:134)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)

And here's the code. Like I said, I haven't made any changes to it, and I don't see the need to make any changes to it based on my project requirements. I hate to go in there mucking things up if it isn't necessary. The entire project is available here: https://github.com/android/platform_packages_apps_alarmclock

I am compiling for API 8.

    // Get the main ListView and remove it from the content view.
    ListView lv = getListView();
    content.removeView(lv);

    // Create the new LinearLayout that will become the content view and
    // make it vertical.
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);

    // Have the ListView expand to fill the screen minus the save/cancel
    // buttons.
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    lp.weight = 1;
    ll.addView(lv, lp); // error here. Line 134

Upvotes: 1

Views: 605

Answers (1)

DigCamara
DigCamara

Reputation: 5578

You could try to get the parent directly from the view so you don't have the problem.

You have

 // Get the main ListView and remove it from the content view.
ListView lv = getListView();
content.removeView(lv);

You'd need to change it to

 // Get the main ListView and remove it from the content view.
ListView lv = getListView();
((ViewGroup)lv.getParent()).removeView(lv);

Upvotes: 3

Related Questions