2016-08-09 94 views
0

Ich bin wirklich verwirrt darüber, was ich mit meinem Code gemacht habe. Es ist nur eine einfache Anwendung Fragmente verwenden, aber ich unten Fehlermeldung erhalten:Fragmente: Aktivität kann nicht gestartet werden. Komponente Info

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sgrumo.sunshineonyourmind/com.example.sgrumo.sunshineonyourmind.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 

Das ist mein MainActivity Klasse:

public class MainActivity extends FragmentActivity { 

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

    if(savedInstanceState==null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new MainActivityFragment()).commit(); 
    } 
} 

Activity-Haupt-XML:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_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" 
    tools:ignore="MergeRootFrame" 
    tools:context="com.example.sgrumo.sunshineonyourmind.MainActivity"> 

Ich wollte nur ein Fragment in meiner Hauptaktivität, was ist falsch an meinem Code?

Dies ist das Fragment Klasse i verwenden:

public class MainActivityFragment extends Fragment { 

    public MainActivityFragment() {} 

    String[] forecastArray = { 
      "Today - Sunny - 31/20", 
      "Tomorrow - Sunny - 42,30", 
      "Wednesday - Rainy - 20,15", 
      "Thursday - FINIMONDO - 35,20" 
    }; 
    ArrayList<String> list = new ArrayList<>(Arrays.asList(forecastArray)); 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,list); 
     ListView lw = (ListView)rootView.findViewById(R.id.listview_forecast); 
     lw.setAdapter(adapter); 
     return rootView; 
    } 

Und seine XML.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_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" 
tools:context="com.example.sgrumo.sunshineonyourmind.MainActivityFragment" 
tools:showIn="@layout/activity_main"> 


<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/listview_forecast" 
    android:layout_gravity="center" /> 

+0

Haben Sie versucht, eine Ansicht hinzuzufügen, die bereits einen Elternteil enthält? Den Code, den Sie teilen, habe ich vorher schon einmal gemacht und es funktioniert gut für mich. Kannst du mehr Code teilen? –

+0

Wie wäre es mit replace() anstelle von add()? – anhtuannd

Antwort

1

Bitte ändern Sie den Code wie in Ihrem onCreateView Verfahren folgende

View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

Sie verfehlten false hinzuzufügen. Vielen Dank.

+0

Das war es! Dumm Fehler, wirklich Danke! Liebe diese Community – sgrumo

+0

Willkommen. Happy codding:) –

0

Verwenden getSupportFragmentManager() begintransaction() .replace (R.id. container() anstelle von getSupportFragmentManager(). beginTransaction() .add (R.id.container, neues MainActiv ityFragment()). commit()

+0

Ich bekomme diese Nachricht: java.lang.IllegalStateException: Das angegebene Kind hat bereits einen Elternteil. Sie müssen removeView() aufrufen – sgrumo

+1

haben Sie versucht, ersetzen statt hinzufügen? –

+0

tat ich. Das ist die Botschaft, die ich bekomme! – sgrumo