Home > Software design >  I try to make A Tablayout But its have an error Please Help me
I try to make A Tablayout But its have an error Please Help me

Time:11-11

Here Is My MainActivity.java

public class MainActivity extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   tabLayout.findViewById(R.id.tabLayout);
   viewPager.findViewById(R.id.viewPager);
   tabLayout.setupWithViewPager(viewPager);

   VPAdapter vpAdapter  = new VPAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    vpAdapter.addFragment(new statistics(),"Statistics");
    vpAdapter.addFragment(new connectivity(),"Connectivity");
    vpAdapter.addFragment(new maskSensor(),"Mask Sensor");
    viewPager.setAdapter(vpAdapter);

} }

Here The MainActivity.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:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.material.tabs.TabLayout
    android:id="@ id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Statistics" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Connectivity" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mask Sensor" />
</com.google.android.material.tabs.TabLayout>

<androidx.viewpager.widget.ViewPager
    android:id="@ id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@ id/tabLayout"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Here My VPAdapter.java

public class VPAdapter extends FragmentPagerAdapter {

private final ArrayList<Fragment> fragmentArrayList = new ArrayList<>();
private final ArrayList<String> fragmentTitle = new ArrayList<>();

public VPAdapter(@NonNull @NotNull FragmentManager fm, int behavior) {
    super(fm, behavior);
}

@NonNull
@NotNull
@Override
public Fragment getItem(int position) {
    return fragmentArrayList.get(position);
}

@Override
public int getCount() {
    return fragmentArrayList.size();
}

public void addFragment(Fragment fragment , String title)
{
    fragmentArrayList.add(fragment);
    fragmentTitle.add(title);
}

@Nullable
@org.jetbrains.annotations.Nullable
@Override
public CharSequence getPageTitle(int position) {
    return fragmentTitle.get(position);
}
}

And This My Logcat

 11-11 16:18:36.645 21916-21916/com.nekat.sippanto E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nekat.sippanto, PID: 21916
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nekat.sippanto/com.nekat.sippanto.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.material.tabs.TabLayout.findViewById(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
        at android.app.ActivityThread.access$800(ActivityThread.java:169)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5549)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.google.android.material.tabs.TabLayout.findViewById(int)' on a null object reference
        at com.nekat.sippanto.MainActivity.onCreate(MainActivity.java:22)
        at android.app.Activity.performCreate(Activity.java:5977)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526) 
        at android.app.ActivityThread.access$800(ActivityThread.java:169) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5549) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759

CodePudding user response:

You are not initializing your TabLayout and ViewPager properly. Change these lines

   tabLayout.findViewById(R.id.tabLayout);
   viewPager.findViewById(R.id.viewPager);

to

tabLayout = findViewById(R.id.tabLayout);
viewPager = findViewById(R.id.viewPager);
  • Related