0

构建基于位置的安卓应用程序。我有我的班级启动 google maps api 工作(MygoogleMapsActivator)。这是这个类的构造函数

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

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

当我想在我的活动中显示它时,我在 LogCat 中得到了这个异常: 引起:java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。我从这个函数中得到 ti:

    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>

谢谢

4

1 回答 1

0

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

于 2013-08-05T17:31:53.443 回答