IllegalArgumentException: не найдено представление для идентификатора (fragments_container) для фрагмента - PullRequest
0 голосов
/ 14 марта 2019

Я искал причину этого исключения в ответах на похожие вопросы и не смог найти, что не так с моим кодом. Я изменил метод, который создает фрагмент несколько раз, и получил то же исключение. Может быть, это как-то связано с заставкой. Не удалось найти информацию об этом.

Вот начало 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();
    }
}

xml MainActivity:

<?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 голосов
/ 14 марта 2019

По какой-то причине ваш основной макет деятельности называется R.layout.activity_splash Я полагаю, что вы хотели, чтобы он был таким, каким называется основной макет деятельности (тот, в котором имеется R.id.fragments_container).

...