Android 指定的子项在google alarmclock代码中已具有父项

Android 指定的子项在google alarmclock代码中已具有父项,android,Android,我正在为我当前的项目修改一些谷歌代码。我刚开始在这个类中遇到一个错误,这是令人费解的,因为我根本没有对这个类做任何更改 以下是错误: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skipmorrow.powerclock/com.skipmorrow.powerclock.SetAlarm}: java.lang.IllegalStateExcepti

我正在为我当前的项目修改一些谷歌代码。我刚开始在这个类中遇到一个错误,这是令人费解的,因为我根本没有对这个类做任何更改

以下是错误:

 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)
这是代码。就像我说的,我没有对它做任何更改,我也不认为有必要根据我的项目需求对它做任何更改。如果没有必要的话,我不愿意进去把事情搞得一团糟。整个项目可在以下位置获得:

我是为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

您可以尝试直接从视图中获取父对象,这样就不会出现问题

你有

 // Get the main ListView and remove it from the content view.
ListView lv = getListView();
content.removeView(lv);
你需要把它改成

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

这就解决了问题。奇怪的是,谷歌代码中竟然有这么简单的东西。哦,好吧。。。谢谢