2013-08-05 65 views
0

構建android,基於位置的應用程序。我有我的班級啓動谷歌地圖api工作(MygoogleMapsActivator)。這是這個類的約束者java.lang.IllegalStateException:指定的子項已具有父項。 (編輯我的帖子後)

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> 

感謝

回答

0

的問題是已經連接到您的視圖層次TextView的。我猜想,使用地址標識的視圖已經在您用於活動的主佈局文件中,而沒有完整的活動代碼很難說。如果這是真的,您應該刪除setContentView調用,因爲它不是必需的。確保你想添加文本到TextView中已經存在的文本中,如果你想替換文本你應該使用setText而不是追加。

+0

感謝您的回覆,我添加了完整的活動代碼 – user2653983

+0

我刪除了它,但仍然是一個例外 – user2653983

相關問題