I'm trying to achieve Fragment to Fragment transaction with onOptionsItemSelected
but I keep getting
java.lang.IllegalArgumentException: No view found for id
I also read this thread: Android Fragment no view found for ID? and applied some answer to my code but it is not solve my problem
Here is my HomeFragment.java
private Toolbar toolbar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//Set Toolbar for Fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
toolbar = (Toolbar) v.findViewById(R.id.main_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
setHasOptionsMenu(true);
return v;
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.toolbar_menu,menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.beginTransaction().replace(R.id.cart_fragment_layout,new CartFragment())
.commitNow();
return super.onOptionsItemSelected(item);
}
}
My fragment_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<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:id="@ id/cart_fragment_layout"
tools:context="com.example.fragment.CartFragment"
android:background="@color/black">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:id="@ id/cart_ll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/cart_rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:textSize="50dp"
android:text="Cart Fragment" />
</LinearLayout>
</FrameLayout>
and my CartFragment.java
public class CartFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_cart, container, false);
return v;
}
}
My MainActivity.java just in case, I set HomeFragment as default:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.frament_container,
new HomeFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_menu:
selectedFragment = new MenuFragment();
break;
case R.id.nav_user:
selectedFragment = new UserFragment();
break;
case R.id.nav_more:
selectedFragment = new MoreFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.frament_container,
selectedFragment).commit();
return true;
}
};
}
CodePudding user response:
The id you use in a fragment transaction is NOT the view id of a view in the fragment. Its the id of the fragment stub view in the layout you're popping the fragment into. So you're passing in the wrong id. I can't tell you what the right one is, as you don't give us the xml of your main layout.
Basically the fragment transaction looks for the id you pass out in the context root layout, then pops out what's already there and replaces it with your fragment.