1

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

这是错误:

 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)

这是代码。就像我说的,我没有对其进行任何更改,并且我认为没有必要根据我的项目要求对其进行任何更改。如果没有必要,我讨厌进去把事情搞砸。整个项目可在此处获得: https ://github.com/android/platform_packages_apps_alarmclock

我正在为 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
4

1 回答 1

3

您可以尝试直接从视图中获取父级,这样就不会遇到问题。

你有

 // 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);
于 2013-03-04T02:15:19.350 回答