0

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.

Zeruma
  • 33
  • 7
  • 1
    It's not the fragment manager but `tabLayout` that is null. It is in your fragment and not your activity, so move the init code to the fragment as well. – laalto Mar 20 '21 at 19:33
  • Expanded comment to an answer – laalto Mar 20 '21 at 19:49

1 Answers1

-1

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());"

This means that tabLayout is null. Nothing to do with getSupportFragmentManager() even though the call is on the same line.

Your tabLayout is in HomeFragment so you should be setting up your tab layout in that fragment, not in your activity.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • But how am I supposed to start this fragment at the start? And do I need to move the whole onClick function that has something to do with this fragment to the fragment's class? – Zeruma Mar 20 '21 at 20:09
  • You can use a fragment transaction to add your fragment to the activity, For fragment view init, the `onViewCreated()` callback is a good place to set up stuff like click listeners. – laalto Mar 20 '21 at 20:16