尝试使用复合视图添加View()时出现IllegalStateException

时间:2015-08-18 15:32:04

标签: java android exception android-view

我想在按下按钮时将LinearLayout添加到主Activity视图中。

注意在使用merge尝试此操作之前,我使用LinearLayout得到了相同的结果。我读了post,这让我merge会有所帮助。

我要添加的View的XML看起来像这样。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/grocery_list_row">

    <EditText
        android:hint="Price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:hint="Item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</merge>

这个View的java看起来像这样

package com.example.developer.grocerylist.impl;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.example.developer.grocerylist.R;

/**
 * Created by developer on 8/16/2015.
 */
public class GroceryListTableRowImpl extends LinearLayout {

    int mRows;

    public GroceryListTableRowImpl(Context context) {
        this(context, null);
    }

    public GroceryListTableRowImpl(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public GroceryListTableRowImpl(Context context, AttributeSet attributeSet, int defStyles) {
        super(context, attributeSet, defStyles);
    }

    public void addRow(){
        mRows++;
        removeAllViews();
        for(int i = 0; i < mRows; i++){
            addView(createRow());
        }
    }

    private View createRow() {
        View v;
        LayoutInflater inflater = LayoutInflater.from(getContext());
        v = inflater.inflate(R.layout.grocery_list_row,this,true);
        return v;
    }
}

Activity的XML看起来像这样

<TableLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".GroceryList"
    android:id="@+id/list_table">

    <com.example.developer.grocerylist.impl.GroceryListTableRowImpl
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/grocery_list">

    </com.example.developer.grocerylist.impl.GroceryListTableRowImpl>


    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <DigitalClock
            android:textSize="20pt"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"/>
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="250px"
            android:textSize="20pt"
            android:paddingBottom="5px"
            android:text=""
            android:background="#777777"
            android:textColor="#ff0000" />

        <Button
            android:id="@+id/add"
            android:text="add" />
    </TableRow>
</TableLayout>

最后是主Activity的java

package com.example.developer.grocerylist.activities;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;

import com.example.developer.grocerylist.R;
import com.example.developer.grocerylist.api.GroceryListTableRow;
import com.example.developer.grocerylist.impl.GroceryListTableRowImpl;


public class GroceryList extends AppCompatActivity implements OnClickListener {

    TextView textView = null;
    GroceryListTableRowImpl groceryListTableRow = null;
    Button add = null;

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

        textView = (TextView)findViewById(R.id.textview);
        groceryListTableRow = (GroceryListTableRowImpl)findViewById(R.id.grocery_list);
        add = (Button)findViewById(R.id.add);
        add.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_grocery_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        groceryListTableRow.addRow();
    }
}

当我启动应用程序并单击按钮时,我得到了这个

Process: com.example.developer.grocerylist, PID: 2353
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:3936)
        at android.view.ViewGroup.addView(ViewGroup.java:3786)
        at android.view.ViewGroup.addView(ViewGroup.java:3727)
        at android.view.ViewGroup.addView(ViewGroup.java:3700)
        at com.example.developer.grocerylist.impl.GroceryListTableRowImpl.addRow(GroceryListTableRowImpl.java:34)
        at com.example.developer.grocerylist.activities.GroceryList.onClick(GroceryList.java:61)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我知道这很重要。我一直在摸不着头脑。我之前从未做过复合观点。这可能很明显。任何帮助将不胜感激。提前谢谢!

UPDATE 当我到达addView(createRow())行时调试失败。它似乎没有进入createRow()方法。

2 个答案:

答案 0 :(得分:0)

根据我在 createRow()方法中的观察更改并尝试此事

ViewGroup parent = (ViewGroup)v.Parent;
return parent

由于您使用了合并,因此存在父视图组,因此要么返回父视图,要么使用

删除父视图
parent.RemoveView(v);
return v;

答案 1 :(得分:0)

感谢@ njkz2

为我修复此问题的原因是将onCreate()方法更改为此。

private View createRow() {
    View v;
    LayoutInflater inflater = LayoutInflater.from(getContext());
    v = inflater.inflate(R.layout.grocery_list_row,this,false);
    return v;
}

此外,我更改了类以扩展TableLayout而非LinearLayout以获得所需的效果。这是因为我将复合视图(LinearLayout)添加到我的GroceryListTableRowImpl。因此GroceryListTableRowImpl必须是TableLayout