BaseExpandableListAdapter指定的子级已有父级

时间:2013-09-04 11:37:45

标签: android

您好我有这段代码:

public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View view, ViewGroup parent) {
    ExpandListChild child = (ExpandListChild) getChild(groupPosition,
            childPosition);
    if (view == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        view = infalInflater.inflate(R.layout.expandlist_child_item, null);
    }

    TextView tv = (TextView) view.findViewById(R.id.child);
    if(child.getName() != null)
        tv.setText(child.getName().toString());

    LinearLayout image = (LinearLayout) view.findViewById(R.id.instructionsHelper);

    RelativeLayout layouts = child.getLayouts();

    if(layouts != null)
    {
        image.removeAllViews(); 
        image.addView(layouts);
    }

    return view;
}

我在这一行上得到错误:

image.addView(layouts);

我不明白为什么我会收到此错误,因为我致电image.removeAllViews();

我的logcat:

09-04 14:33:34.366: E/AndroidRuntime(8585): FATAL EXCEPTION: main
09-04 14:33:34.366: E/AndroidRuntime(8585): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewGroup.addView(ViewGroup.java:3210)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewGroup.addView(ViewGroup.java:3155)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewGroup.addView(ViewGroup.java:3131)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at com.example.workoutlog.ExpandListAdapter.getChildView(ExpandListAdapter.java:71)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at org.holoeverywhere.widget.ExpandableListConnector.getView(ExpandableListConnector.java:469)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at org.holoeverywhere.widget.ListAdapterWrapper.getView(ListAdapterWrapper.java:83)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.widget.AbsListView.obtainView(AbsListView.java:2143)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.widget.ListView.onMeasure(ListView.java:1158)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.View.measure(View.java:15525)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.View.measure(View.java:15525)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.View.measure(View.java:15525)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.View.measure(View.java:15525)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at org.holoeverywhere.widget.LinearLayout.forceUniformWidth(LinearLayout.java:286)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at org.holoeverywhere.widget.LinearLayout.measureVertical(LinearLayout.java:1065)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at org.holoeverywhere.widget.LinearLayout.onMeasure(LinearLayout.java:1105)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.View.measure(View.java:15525)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at org.holoeverywhere.internal.WindowDecorView.onMeasure(WindowDecorView.java:114)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.View.measure(View.java:15525)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.View.measure(View.java:15525)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.View.measure(View.java:15525)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2434)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.View.measure(View.java:15525)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.Choreographer.doFrame(Choreographer.java:532)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.os.Handler.handleCallback(Handler.java:725)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.os.Looper.loop(Looper.java:137)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at android.app.ActivityThread.main(ActivityThread.java:5226)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at java.lang.reflect.Method.invokeNative(Native Method)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at java.lang.reflect.Method.invoke(Method.java:511)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
09-04 14:33:34.366: E/AndroidRuntime(8585):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

见这里

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

首先你应该layout.removeView(layouts)

然后您将layouts分配给任何其他父级。 因为你不能同时给两个父母分配东西。