片段:无法启动活动组件信息

时间:2016-08-09 11:01:38

标签: android android-studio android-fragments

我对我的代码所做的事情感到很困惑。 它只是一个使用Fragments的简单应用程序,但我得到以下错误:

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)

这是我的MainActivity类:

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-main 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">

我只是想在我的活动主页中找到一个片段,我的代码出了什么问题?

这是我使用的Fragment类:

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;
    }

及其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" />

2 个答案:

答案 0 :(得分:1)

请在onCreateView方法

中更改以下代码
View  rootView = inflater.inflate(R.layout.fragment_main, container, false);

您错过了添加false。感谢。

答案 1 :(得分:0)

使用getSupportFragmentManager()。beginTransaction()                 .replace(R.id.container,new MainActivityFragment())。commit()而不是getSupportFragmentManager()。beginTransaction()                 .add(R.id.container,new MainActivityFragment())。commit()