the problem might be obvious, but I have still problems to find it. I've created the main screen in two different parts, the "toolbar" and the "fragmentLayout". The problem is, I get an error "java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.material.tabs.TabLayout.getTabCount()' on a null object reference" whenever I want to execute this command: "pageAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());"
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private TabLayout tabLayout;
private ViewPager viewPager;
private TabItem tab1, tab2;
public PageAdapter pageAdapter;
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.nav_home);
}
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tab1 = (TabItem) findViewById(R.id.tab1);
tab2 = (TabItem) findViewById(R.id.tab2);
viewPager = (ViewPager) findViewById(R.id.viewPager);
pageAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pageAdapter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if(tab.getPosition() == 0)
{
pageAdapter.notifyDataSetChanged();
}
else if(tab.getPosition() == 1)
{
pageAdapter.notifyDataSetChanged();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
And this is the .xml code of the mainActivity:
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="false"
tools:context=".MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#5E5E5E"
android:id="@+id/nav_view"
app:headerLayout="@layout/main_nav_drawer"
app:menu="@menu/drawer_menu"
app:itemTextColor="@android:color/white"
app:itemTextAppearance="@style/NavText"/>
</androidx.drawerlayout.widget.DrawerLayout>
And this is the .xml of the "HomeFragment":
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:layout_constraintBottom_toTopOf="parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="@color/beige">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@drawable/tab_background"
app:tabIndicatorColor="@color/black"
app:tabSelectedTextColor="@color/black">
<com.google.android.material.tabs.TabItem
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/recipes_tab" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fav_tab" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.viewpager.widget.ViewPager>
I appreciate every help.