java.lang.IllegalStateException:指定的子级已有父级。 (编辑我的帖子后)

时间:2013-08-05 17:25:35

标签: android

构建android,基于位置的应用程序。我的班级启动了谷歌地图api工作(MygoogleMapsActivator)。这是这个类的约束

public MyGoogleMapsActivator(Context mContext)
    {
          this.mContext = mContext;
    }

。 上下文mContext是一个活动,我需要显示地图和地址,在这里传递它以获取我需要激活的服务。显示结果的活动 - 我的位置和地址(我从MyGoogleMapsActivator获取的位置和地址)

当我想在我的活动中显示它时,我在LogCat中得到此异常:引起:java.lang.IllegalStateException:指定的子节点已经有父节点。您必须首先在孩子的父母上调用removeView()。 我正在接受这个功能:

    public void SetUpTextViewWithAddress()
        {
             text = (TextView) findViewById(R.id.address);          
             text.append(addressString);
             setContentView(text);
        }

How can i solved it,thanks.
This is full code of my activity:


    protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_add_new_place);

                mGoogleMapsActivator = new MyGoogleMapsActivator(this);

                Button buttonAddPlace = (Button) findViewById(R.id.buttonAddPlace);
                Button buttonAddByAddress = (Button) findViewById(R.id.buttonAddByAddress);

                fm = (SupportMapFragment ) getSupportFragmentManager().findFragmentById(R.id.map);
                buttonAddPlace.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(AddNewPlaceActivity.this, AddByAddress.class);

                        mMyPlace = new Places("", city, street, number, "", LoggedWithFacebookMainActivity.GetLoggedInUserName(), myPosition.latitude, myPosition.longitude);
                        intent.putExtra("Place", mMyPlace);
                        //intent.putExtra("TheStreet", street);
                        //intent.putExtra("TheNumber", number);
                        intent.putExtra("Clarification");
                        startActivity(intent);
                    }
                });

                buttonAddByAddress.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(AddNewPlaceActivity.this, AddByAddress.class);

                        mMyPlace = new Places("", city, street, number, "", LoggedWithFacebookMainActivity.GetLoggedInUserName(), myPosition.latitude, myPosition.longitude);
                        intent.putExtra("Place", mMyPlace);
    //                  intent.putExtra("TheStreet", "Street");
    //                  intent.putExtra("TheNumber", "Number");
                        intent.putExtra("Clarification");
                        startActivity(intent);
                    }
                });

                mGoogleMapsActivator.SetUpMapIfNeed(fm);
            mGoogleMapsActivator.SetMyGoggleActivatorLocationEnabled();
            mGoogleMapsActivator.SetUpService();
            mGoogleMapsActivator.setUpListener();
            mGoogleMapsActivator.SetUpAndGetProvider();
            mGoogleMapsActivator.SetMyLocationWithListener();
            mGoogleMapsActivator.SetMyPosition();
            mGoogleMapsActivator.SetUpMyMarker();   
            mGoogleMapsActivator.SetUpMapView();
            mGoogleMapsActivator.SetUpAddressFromGeocoder();
            city = mGoogleMapsActivator.getCity();
            street = mGoogleMapsActivator.getStreet();
            number = mGoogleMapsActivator.getNumber();
            addressString = mGoogleMapsActivator.getAddressString();
            SetUpTextViewWithAddress();
            mGoogleMapsActivator.AnimateCamera();

  public void SetUpTextViewWithAddress()
    {
         text = (TextView) findViewById(R.id.address);          
         text.setText(addressString);
        // setContentView(text);
    }

}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="end"
    tools:context=".AddNewPlaceActivity" >

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="370dp" />

<TextView
    android:id="@+id/address"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/buttonAddByAddress"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/map"
    android:layout_toLeftOf="@+id/buttonAddPlace"
    android:text="@string/place_address_he" />

<Button
    android:id="@+id/buttonAddPlace"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/buttonAddByAddress"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/address"
    android:background="@drawable/custom_button_main"
    android:paddingBottom="10dp"
    android:text="@string/add_place_he" />

<Button
    android:id="@+id/buttonAddByAddress"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:background="@drawable/custom_button_main"
    android:paddingTop="10dp"
    android:text="@string/add_place_manually_he" />

</RelativeLayout>

由于

1 个答案:

答案 0 :(得分:0)

问题是TextView已附加到您的视图层次结构中。我猜测带有地址ID的视图已经在您用于活动的主要布局文件中,没有完整的活动代码,这很难说。如果这是真的,你应该删除setContentView调用,因为它不需要。确保要将文本附加到TextView中可能已存在的文本中,如果要替换文本,则应使用setText而不是append。