Я уже рассмотрел некоторые другие вопросы, касающиеся этой ошибки, но я либо полностью не понял их, либо они не работают для моего решения, поэтому я хотел бы задать его здесь.
Я получаю ошибку, упомянутую в заголовке, при попытке нажать кнопку для go от одного фрагмента к другому.
Код кнопки:
mTextViewShowUploads.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openImagesActivity();
}
});
Go из одного фрагмента в другой код:
private void openImagesActivity() {
ProfileFragment profileFragment = new ProfileFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.profile_frame, profileFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Макет второго фрагмента
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProfileFragment"
android:id="@+id/profile_frame"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/changeProfilePicBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change profile picture"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26999998" />
<ImageView
android:id="@+id/profilePicView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/changeProfilePicBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@tools:sample/avatars" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="409dp"
android:layout_height="467dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Код макета активности
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:id="@+id/mainView"
tools:visibility="visible"
>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_nav"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/nav_item_colors"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/nav_items">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_above="@id/main_nav"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
Код активности:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
homeFragment = new HomeFragment();
weatherFragment = new WeatherFragment();
uploadFragment = new UploadFragment();
searchFragment = new SearchFragment();
profileFragment = new ProfileFragment();
setFragment(homeFragment);
mMainNav.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home :
setFragment(homeFragment);
return true;
case R.id.nav_weather :
setFragment(weatherFragment);
return true;
case R.id.nav_upload :
setFragment(uploadFragment);
return true;
case R.id.nav_search :
setFragment(searchFragment);
return true;
case R.id.nav_profile :
setFragment(profileFragment);
return true;
default:
return false;
}
}
});
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
@Override
protected void onStart() {
super.onStart();
if(!(FirebaseAuth.getInstance().getCurrentUser() != null)) {
Intent i = new Intent (HomeActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
}
}