Hello everyone I am getting this error when trying to load a fragment when clicking a menu item to my application java.lang.IllegalArgumentException: No view found for id 0x7f0901d1 (com.example.testapp:id/signinfragment) for fragment Signin{79bb46d (c6057ec5-e830-4343-a89f-73175f885aa6) id=0x7f0901d1}
I tried to load the fragment by using the id from the signin fragment I have created but the error still appears.
Main Activity.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_login:
getSupportFragmentManager().beginTransaction().replace(R.id.signinfragment,new Signin()).commit();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
Signin.java
public class Signin extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public Signin() {
// Required empty public constructor
}
public static Signin newInstance(String param1, String param2) {
Signin fragment = new Signin();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_signin, container, false);
}
}
fragment_signin.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/signinfragment"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Signin"
android:orientation="horizontal" />
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/linlay_0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/app_background"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/nav_view"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu"
android:layout_gravity="start"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
the correct usage is:
getSupportFragmentManager().beginTransaction().replace(R.id.id_of_view_to_be_replaced,new Signin()).commit();
So in your activity where you want to laod the fragment you need a view in your layout that you will replace with the fragment.