gpt4 book ai didi

Android - ArrayAdapter : The specified child already has a parent. 您必须先在 child 的 parent 身上调用 removeView()

转载 作者:行者123 更新时间:2023-11-29 20:56:23 26 4
gpt4 key购买 nike

我正在使用 ArrayAdapter 和 LinearLayout 作为通用:

public class TestAdapter extends ArrayAdapter<LinearLayout> {

private Context context;

public TestAdapter(Context context) {
super(context, R.layout.row_result);
this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View rowView = convertView;
ViewHolder viewHolder;

// reuse views
if (rowView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
rowView = inflater.inflate(R.layout.row_result, parent, false);
// configure view holder
viewHolder = new ViewHolder();
viewHolder.row = (FrameLayout) rowView.findViewById(R.id.result_row);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.row.removeAllViews();
viewHolder.row.addView(getItem(position));
return rowView;
}

private static class ViewHolder {
FrameLayout row;
}}

我的问题是:指定的 child 已经有 parent 。您必须先对 child 的 parent 调用 removeView()。

LinearLayout 是动态创建的 imageView 的水平包装器。

这是我的错误。似乎在:viewHolder.row.addView(getItem(position));

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:3577)
at android.view.ViewGroup.addView(ViewGroup.java:3430)
at android.view.ViewGroup.addView(ViewGroup.java:3375)
at android.view.ViewGroup.addView(ViewGroup.java:3351)
at com.dreamstime.buyer.picture.adapter.TestAdapter.getView(TestAdapter.java:43)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
at android.widget.AbsListView.obtainView(AbsListView.java:2308)
at android.widget.ListView.makeAndAddView(ListView.java:1790)
at android.widget.ListView.fillDown(ListView.java:691)
at android.widget.ListView.fillFromTop(ListView.java:752)
at android.widget.ListView.layoutChildren(ListView.java:1630)
at android.widget.AbsListView.onLayout(AbsListView.java:2132)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1673)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14858)
at android.view.ViewGroup.layout(ViewGroup.java:4646)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2035)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1792)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1047)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5896)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

改变这个:

rowView = inflater.inflate(R.layout.row_result, parent, false);

为此:

rowView = inflater.inflate(R.layout.row_result, null, false);

关于Android - ArrayAdapter<LinearLayout> : The specified child already has a parent. 您必须先在 child 的 parent 身上调用 removeView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27578753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com