您必须调用removeView

时间:2014-01-29 23:51:08

标签: android

我已经尝试了几乎关于这个问题的所有主题,并没有解决我的问题。 好。我有一些带有一些视图的TableLayout,当我从另一个Activity中读取一些信息时,我希望更多地添加一些视图。这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.interview);

    params.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
    table = (TableLayout) findViewById(R.id._tabe_inscription_client);
    table.setStretchAllColumns(true);
    table.setShrinkAllColumns(true); 
            rowClient = new TableRow(this);
    TextView client = new TextView(this);
    client.setText("Cliente empresa");
    client.setTextSize(18);
    client.setGravity(Gravity.LEFT);
    client.setTypeface(null, Typeface.BOLD);
    rowClient.setLayoutParams(params);
    rowClient.addView(client, params);
    TableRow roweClient = new TableRow(this);
    EditText eClient = new EditText(this);
    eClient.setLayoutParams(params);
    eClient.setTextSize(18);
    eClient.setGravity(Gravity.LEFT);
    eClient.setText("Escriba el nombre de la empresa");
    eClient.setInputType(InputType.TYPE_CLASS_TEXT);
    roweClient.addView(eClient, params);


              ...more views...

            table.addView(rowClient);
    table.addView(roweClient);
    table.addView(rowContact);
    table.addView(roweContact);
    table.addView(rowInterested);
    table.addView(rowEmailContact);
    table.addView(roweEmailContact);
    table.addView(rowCellphoneContact);
    table.addView(roweCellphoneContact);
    table.addView(rowProductst);

到此为止,一切都有效。这是我需要添加视图的地方。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        Toast.makeText(this, "Productos Recibidos", Toast.LENGTH_SHORT)
                .show();
        if (data != null) {
            Bundle bundle = data.getExtras();
            child = bundle.getStringArrayList("child");
            isChecked = bundle.getBooleanArray("selected");
            table.addView(rowProductst); // here is my problem  
        }
    }


}

我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" 
android:id="@+id/_host_layout">

<TableLayout
    android:id="@+id/_tabe_inscription_client"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" >
</TableLayout>

日志:

致命的例外:主要  java.lang.RuntimeException:将结果ResultInfo {who = null,request = 1,result = 1,data = Intent {(has extras)}}传递给activity {com.telconnet.ventas / com.telconnet.ventas.pre_sale。 Interview}:java.lang.IllegalStateException:指定的子节点已经有父节点。您必须首先在孩子的父母上调用removeView()。     在android.app.ActivityThread.deliverResults(ActivityThread.java:3363)     在android.app.ActivityThread.handleSendResult(ActivityThread.java:3406)     在android.app.ActivityThread.access $ 1100(ActivityThread.java:145)     在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1294)     在android.os.Handler.dispatchMessage(Handler.java:99)     在android.os.Looper.loop(Looper.java:137)     在android.app.ActivityThread.main(ActivityThread.java:5095)     at java.lang.reflect.Method.invokeNative(Native Method)     在java.lang.reflect.Method.invoke(Method.java:511)     在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:898)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)     at dalvik.system.NativeStart.main(Native Method)  引起:java.lang.IllegalStateException:指定的子节点已经有父节点。您必须首先在孩子的父母上调用removeView()。     在android.view.ViewGroup.addViewInner(ViewGroup.java:3345)     在android.view.ViewGroup.addView(ViewGroup.java:3216)     在android.widget.TableLayout.addView(TableLayout.java:429)     在android.view.ViewGroup.addView(ViewGroup.java:3161)     在android.widget.TableLayout.addView(TableLayout.java:411)     在android.view.ViewGroup.addView(ViewGroup.java:3137)     在android.widget.TableLayout.addView(TableLayout.java:402)     在com.telconnet.ventas.pre_sale.Interview.onActivityResult(Interview.java:202)     在android.app.Activity.dispatchActivityResult(Activity.java:5314)     在android.app.ActivityThread.deliverResults(ActivityThread.java:3359)     ......还有11个

这是我的帖子,所以我希望以正确的方式制作。 谁能帮我??请。

3 个答案:

答案 0 :(得分:0)

编辑:

您已添加rowProductst视图,并在返回活动时再次添加,并触发onActivityResult

///////

将新视图添加到布局后,您必须调用setContentView

答案 1 :(得分:0)

这里的另一种选择。在onActivityResult中,您需要为要插入的元素为新View充气,例如:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        Toast.makeText(this, "Productos Recibidos", Toast.LENGTH_SHORT)
                .show();
        if (data != null) {
            Bundle bundle = data.getExtras();
            child = bundle.getStringArrayList("child");
            isChecked = bundle.getBooleanArray("selected");
            // We will inflate the view with the table as a parent but not append it
            View newView = LayoutInflater.from(getBaseContext())
                .inflate(R.layout.your_row_layout, table, false);
            // Refresh the data in your view
            // ...
            // Finally add it to the table
            table.addView(newView); // here is my problem  
        }
    }
}

我也注意到很多硬编码的字符串,你应该尽量避免这种做法。

答案 2 :(得分:0)

首先:

  1. 尽量避免在代码中明确定义布局。你可以在xml中明确地定义它并加载它(你的代码会缩短)。

  2. 如您所见,ViewGroup中抛出异常(addViewInnter方法负责抛出此异常):

    private void addViewInner(View child, int index, LayoutParams params,
        boolean preventRequestLayout) {
    
    if (mTransition != null) {
        // Don't prevent other add transitions from completing, but cancel remove
        // transitions to let them complete the process before we add to the container
        mTransition.cancel(LayoutTransition.DISAPPEARING);
    }
    
    if (child.getParent() != null) {
        throw new IllegalStateException("The specified child already has a parent. " +
                "You must call removeView() on the child's parent first.");
    ...
    

    }

  3. 显然,您在onActivityResult中添加的rowProductst视图已包含父视图。克服这个障碍:

    1. 找出rowProductst具有父视图的原因。你必须简单地调用:

        ((ViewGroup)parentView).removeView(rowProductst);
      
    2. 再次添加。

      1. 您也可以通过从上下文调用LAYOUT_INFLATER_SERVICE来扩充新视图(context.getSystemService(LAYOUT_INFLATER_SERVICE)),这肯定会有效。
      2. 请注意,您的代码看起来不太好。