问题:指定的子已具有父。您必须先对孩子的父母打电话removeView() [英] issue: The specified child already has a parent. You must call removeView() on the child's parent first

查看:243
本文介绍了问题:指定的子已具有父。您必须先对孩子的父母打电话removeView()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

荫开发的应用程序将动态数据添加到多个表views.The结构如下。


在onPostExecute我的code是:

 保护无效onPostExecute(列表<串GT;业绩){
INT IND,I = 0;
滚动型SV = NULL;
TableLayout TL = NULL;
TR的TableRow = NULL;
INT mjsonlength = results.size();
如果(结果!= NULL){ TableLayout indextl =(TableLayout)findViewById(R.id.indextablelayout);
    的TableRow indextr =新的TableRow(SubProducts.this);
    //indext1.moveToFirst();
    LLT的LinearLayout =(的LinearLayout)findViewById(R.id.alltablell);
    //为(INT F = 0; F< mjsonlength;˚F++){        为(IND = 0;的ind&下; indextitle.size(); IND ++)
        {            TextView的indextv =新的TextView(SubProducts.this);
            indextv.setBackgroundResource(R.drawable.greenbg);
            indextv.setHeight(25);
            indextv.setWidth(50);
            indextv.setId(6);
            indextv.setTextColor(Color.WHITE);
            indextv.setText(indextitle.get(IND));
            indextv.setPadding(20,10,6,3);
            indextr.addView(indextv);            indextl.addView(indextr,6);            SV =新的滚动型(SubProducts.this);
             TL =新TableLayout(SubProducts.this);
             对于(i = 0; I< mjsonlength;我++){
                    TR =新的TableRow(SubProducts.this);                    如果(IND == I){                        RelativeLayout的RL =新的RelativeLayout(SubProducts.this);
                        资源解析度= getResources(); //资源句柄
                          rl.setBackgroundResource(R.drawable.prd_box);                         RelativeLayout.LayoutParams newParams1 =新RelativeLayout.LayoutParams(
                                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);                            图像=新ImageView的(SubProducts.this);
                            image.setLayoutParams(newParams1);
                            image.setId(1);
                            image.setImageBitmap(BMP);
                            image.setPadding(5,20,5,5);
                            rl.addView(图像,1);                          RelativeLayout.LayoutParams lprams =新RelativeLayout.LayoutParams(
                                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);
                            lprams.addRule(RelativeLayout.RIGHT_OF,1);                          TextView的TV1 =新的TextView(SubProducts.this);
                        跨区marked_up = Html.fromHtml(description.get(I));
                        tv1.setText((marked_up.toString())子(1,170).replaceAll(与& NBSP;,));
                        tv1.setLayoutParams(lprams);
                        tv1.setId(2);
                        tv1.setHeight(150);
                        tv1.setWidth(130);
                        tv1.setPadding(20,10,0,10);
                        rl.addView(tv1,2);
                         RelativeLayout.LayoutParams newParams =新RelativeLayout.LayoutParams(
                                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);
                         newParams.addRule(RelativeLayout.BELOW,2);
                        TextView的电视=新的TextView(SubProducts.this);
                        tv.setText(productname.get(I));
                        tv.setLayoutParams(newParams);
                        tv.setTextColor(Color.WHITE);
                        tv.setId(3);
                        tv.setPadding(10,0,0,10);
                        rl.addView(电视,3);
                        tr.addView(RL);
                        tr.setPadding(5,10,5,10);
                    }
                    tl.addView(TR);            }             sv.addView(TL);                llt.addView(SV);        }    }

}

荫收到错误如下:

  6月3日至16日:27:07.452:E / AndroidRuntime(7233):致命异常:主要
      六月3日至16日:27:07.452:E / AndroidRuntime(7233):java.lang.IllegalStateException:在六月3日至16日:27:07.452:E / AndroidRuntime(7233):致命异常:主要
六月3日至16日:27:07.452:E / AndroidRuntime(7233):java.lang.IllegalStateException:指定的小孩已经有一个父。您必须先对孩子的父母打电话removeView()。
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.view.ViewGroup.addView(ViewGroup.java:3210)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.widget.TableLayout.addView(TableLayout.java:429)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.view.ViewGroup.addView(ViewGroup.java:3155)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.widget.TableLayout.addView(TableLayout.java:411)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.view.ViewGroup.addView(ViewGroup.java:3131)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.widget.TableLayout.addView(TableLayout.java:402)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在com.example.nutritionplusapp.SubProducts $ LongRunningGetIO1.onPostExecute(SubProducts.java:227)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在com.example.nutritionplusapp.SubProducts $ LongRunningGetIO1.onPostExecute(SubProducts.java:1)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.os.AsyncTask.finish(AsyncTask.java:631)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.os.AsyncTask.access $ 600(AsyncTask.java:177)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.os.Handler.dispatchMessage(Handler.java:99)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.os.Looper.loop(Looper.java:137)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在android.app.ActivityThread.main(ActivityThread.java:5041)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在java.lang.reflect.Method.invokeNative(本机方法)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在java.lang.reflect.Method.invoke(Method.java:511)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
六月3日至16日:27:07.452:E / AndroidRuntime(7233):在dalvik.system.NativeStart.main(本机方法)
六月3日至16日:32:07.586:I /流程(7233):发送信号。 PID:7233 SIG:9
六月3日至16日:32:08.522:E /跟踪(7355):错误打开跟踪文件:没有这样的文件或目录(2)

循环变量值应该是相同的两个循环中序得到相应product.Iam的子产品与上述error.Anybody战斗有idea.Plz建议我适当solution.Thanks的进展。

编辑:

 的ArrayList<串GT;产品名称=新的ArrayList<串GT;();
ArrayList的<串GT;说明=新的ArrayList<串GT;();
ArrayList的<串GT; indextitle =新的ArrayList<串GT;();
 ArrayList的<串GT; mStrings =新的ArrayList<串GT;();

doINBackground下:

  JSONObject的JSON = array.getJSONObject(I)     manJson = json.getJSONObject(产品);
     mStrings.add(manJson.getString(productimage));
     productname.add(manJson.getString(产品名));
    indextitle.add(manJson.getString(subcategoryid));
    description.add(manJson.getString(说明));


解决方案

好吧第二个潜在的问题也是:
indextl.addView(indextr);

indextr被添加的每一个循环迭代,但它同样的观点。您只能一次层次添加视图。你可能想为每个循环迭代一个新的实例。我不能肯定地说,但这是有可能的问题。

Iam developing app to add dynamic data to multiple table views.The structure is as follows.

My code in onPostExecute is :

    protected void onPostExecute(List<String> results){
int ind,i = 0;
ScrollView sv=null;
TableLayout tl=null;
TableRow tr = null;
int mjsonlength=results.size();
if(results!=null){

 TableLayout indextl=(TableLayout)findViewById(R.id.indextablelayout);
    TableRow indextr=new TableRow(SubProducts.this);
    //indext1.moveToFirst();
    LinearLayout llt=(LinearLayout)findViewById(R.id.alltablell);
    //for(int f=0;f<mjsonlength;f++){

        for( ind=0;ind<indextitle.size();ind++)
        {

            TextView indextv=new TextView(SubProducts.this);
            indextv.setBackgroundResource(R.drawable.greenbg);
            indextv.setHeight(25);
            indextv.setWidth(50);
            indextv.setId(6);
            indextv.setTextColor(Color.WHITE);
            indextv.setText(indextitle.get(ind));
            indextv.setPadding(20,10,6,3);
            indextr.addView(indextv);

            indextl.addView(indextr,6);

            sv=new ScrollView(SubProducts.this);
             tl=new TableLayout(SubProducts.this);
             for( i=0;i<mjsonlength;i++){
                    tr=new TableRow(SubProducts.this);

                    if(ind==i)  {

                        RelativeLayout rl=new RelativeLayout(SubProducts.this);
                        Resources res = getResources(); //resource handle
                          rl.setBackgroundResource(R.drawable.prd_box);

                         RelativeLayout.LayoutParams newParams1 = new RelativeLayout.LayoutParams(
                                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);

                            image=new ImageView(SubProducts.this);
                            image.setLayoutParams(newParams1);
                            image.setId(1);
                            image.setImageBitmap(bmp);
                            image.setPadding(5, 20, 5, 5);
                            rl.addView(image,1);



                          RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
                                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);
                            lprams.addRule(RelativeLayout.RIGHT_OF, 1);

                          TextView tv1=new TextView(SubProducts.this);
                        Spanned marked_up = Html.fromHtml(description.get(i));
                        tv1.setText((marked_up.toString()).substring(1, 170).replaceAll("&nbsp;",""));
                        tv1.setLayoutParams(lprams);
                        tv1.setId(2);
                        tv1.setHeight(150);
                        tv1.setWidth(130);
                        tv1.setPadding(20, 10, 0, 10);
                        rl.addView(tv1,2);


                         RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
                                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);
                         newParams.addRule(RelativeLayout.BELOW, 2);
                        TextView tv=new TextView(SubProducts.this);
                        tv.setText(productname.get(i));
                        tv.setLayoutParams(newParams);
                        tv.setTextColor(Color.WHITE);
                        tv.setId(3);
                        tv.setPadding(10, 0, 0, 10);
                        rl.addView(tv,3);


                        tr.addView(rl);
                        tr.setPadding(5, 10, 5, 10);


                    }
                    tl.addView(tr);

            }

             sv.addView(tl);

                llt.addView(sv);

        }

    }

}

Iam getting error as follows:

     03-16 06:27:07.452: E/AndroidRuntime(7233): FATAL EXCEPTION: main
      03-16 06:27:07.452: E/AndroidRuntime(7233): java.lang.IllegalStateException: The 03-16 06:27:07.452: E/AndroidRuntime(7233): FATAL EXCEPTION: main
03-16 06:27:07.452: E/AndroidRuntime(7233): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.view.ViewGroup.addView(ViewGroup.java:3210)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.widget.TableLayout.addView(TableLayout.java:429)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.view.ViewGroup.addView(ViewGroup.java:3155)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.widget.TableLayout.addView(TableLayout.java:411)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.view.ViewGroup.addView(ViewGroup.java:3131)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.widget.TableLayout.addView(TableLayout.java:402)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at com.example.nutritionplusapp.SubProducts$LongRunningGetIO1.onPostExecute(SubProducts.java:227)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at com.example.nutritionplusapp.SubProducts$LongRunningGetIO1.onPostExecute(SubProducts.java:1)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.os.AsyncTask.finish(AsyncTask.java:631)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.os.Looper.loop(Looper.java:137)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at java.lang.reflect.Method.invoke(Method.java:511)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-16 06:27:07.452: E/AndroidRuntime(7233):     at dalvik.system.NativeStart.main(Native Method)
03-16 06:32:07.586: I/Process(7233): Sending signal. PID: 7233 SIG: 9
03-16 06:32:08.522: E/Trace(7355): error opening trace file: No such file or directory (2)

The loop variable value should be same for both loops Inorder get subproducts of respective product.Iam fighting with the above error.Anybody having idea.Plz suggest me with proper solution.Thanks in Advance.

EDIT:

ArrayList<String> productname=new ArrayList<String>();
ArrayList<String> description=new ArrayList<String>();
ArrayList<String> indextitle=new ArrayList<String>();
 ArrayList<String> mStrings = new ArrayList<String>();

under doINBackground:

 JSONObject json = array.getJSONObject(i);

     manJson = json.getJSONObject("product");
     mStrings.add(manJson.getString("productimage"));
     productname.add(manJson.getString("productname"));
    indextitle.add(manJson.getString("subcategoryid"));
    description.add(manJson.getString("description"));

解决方案

Okay the second potential problem is also: indextl.addView(indextr);

indextr gets added every single loop iteration but it's the same view. You can only add a view once to the hierarchy. You probably want a new instance for each loop iteration. I can't say for sure but this is likely the problem.

这篇关于问题:指定的子已具有父。您必须先对孩子的父母打电话removeView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆