IllegalArgumentException:找不到片段的ID(fragments_container)的视图

时间:2019-03-14 12:42:28

标签: java android android-fragments fragment

我一直在寻找类似问题的答案中的异常原因,但找不到我的代码出了什么问题。我更改了实例化片段几次的方法,并得到了相同的异常。也许与启动屏幕有关。找不到有关的信息。

这是 MainActivity 的开头,其中实例化片段的方法称为:

public class MainActivity extends AppCompatActivity {

Context context;

FragmentTransaction ft;
Fragment mlf;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    File file = new File(path);
    if (file.exists()) {
    Log.d(TAG, path + " exists");
    new DBConnection(MainActivity.this);
    if(savedInstanceState == null) {
        toMovieListFragment();
    }
  //...
//...

以下是方法:

private void toMovieListFragment() {

    mlf = new MovieListFragment();

    ft = getSupportFragmentManager().beginTransaction();

    ft.replace(R.id.fragments_ontainer, mlf);
    ft.addToBackStack(null); // add to back stack
    ft.commit();
    }
}

MainActivity的xml:

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout 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"
tools:context=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/fragments_container">
</LinearLayout>

片段类:

public class MovieListFragment extends Fragment {

Context context;
TextView listTtl;
RecyclerView rvMovies;
Adapter moviesAdapter;
List<Movie> moviesList;

Fragment mdf;
FragmentTransaction ft;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

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

    context = getActivity();

    listTtl = rootView.findViewById(R.id.moviesListTtlId);

    rvMovies = rootView.findViewById(R.id.moviesRVId);
    moviesList = new ArrayList<>();
    moviesAdapter = new Adapter(context, moviesList);
    rvMovies.setAdapter(moviesAdapter);

    // setting a layout manager
    rvMovies.setLayoutManager(new LinearLayoutManager(context)); // a regular one
    Log.i(TAG,"In movieListFragment");
    return rootView;

}
}

片段类的xml文件:

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/moviesListTtlId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/dot_height"
        android:text="@string/moviesListTitle"
        android:textAlignment="center"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="@dimen/ttlSize" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/moviesRVId"
        android:layout_margin="@dimen/dimen_10"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

<include layout="@layout/change_name" />

谢谢。

1 个答案:

答案 0 :(得分:0)

出于某种原因,您的主要活动布局被称为// Old function func Foo(a int) // Updated to: func Foo(a int, params ...string) ,我相信您的意图是使它成为任何主要活动布局(内部具有R.id.fragments_container的布局)。