I'm trying to display two fragments with BottomNaviagtionView one is HomeFragment and second is AboutFragment. 1st one is Ho and 2nd mefragment which also has tablayout and contains two fragments which is working properly. But now I want 2 only main fragments from bottomNavigationview 1st HomeFragment and 2nd Aboutfragment.
HomeFragment.java
public class HomeFragment extends Fragment {
FloatingActionButton add;
TabLayout tabLayout;
ViewPager2 viewPager2;
MyAdapter myAdapter;
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
add = getActivity().findViewById(R.id.addButton);
tabLayout = getActivity().findViewById(R.id.tabLayout);
viewPager2 = getActivity().findViewById(R.id.viewPager);
myAdapter = new MyAdapter(this);
viewPager2.setAdapter(myAdapter);//this is where error occured
//Attempt to invoke virtual method 'void androidx.viewpager2.widget.ViewPager2.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
add.setOnClickListener(view -> {
Intent intent = new Intent(getActivity().getApplicationContext(), AddMesurement.class);
startActivity(intent);
});
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
tabLayout.getTabAt(position).select();
}
});
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/homeFragment"
tools:context=".HomeFragment">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:elevation="3dp"
app:borderWidth="0dp"
android:src="@drawable/ic_baseline_note_add_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:backgroundTint="@color/purple_200"
android:contentDescription="@string/app_name" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/recent"
android:text="RECENT"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/confirmed"
android:text="CONFIRMED"/>
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"
app:layout_constraintTop_toBottomOf="@+id/tabLayout"/>
</androidx.constraintlayout.widget.ConstraintLayout>
//this is my main java file which is having bottom bar and I am accessing fregments from this file. Homepage.java
public class HomePage extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
bottomNavigationView = findViewById(R.id.bottomBar);
bottomNavigationView.setSelectedItemId(R.id.home);
fragmentReplace(new HomeFragment());
bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case (R.id.home):
fragmentReplace(new HomeFragment());
break;
case (R.id.about):
fragmentReplace(new AboutFragment());
break;
default:
fragmentReplace(new HomeFragment());
break;
}
return true;
}
});
}
private void fragmentReplace(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit();
}
}