Resolve the "Cannot call ViewPagerAdapter constructor" error by ensuring your adapter class extends FragmentStateAdapter or FragmentPagerAdapter and that you are instantiating it with a valid FragmentActivity or FragmentManager. This typically occurs when passing an incorrect context type or failing to implement required abstract methods like getItem() and getCount().
Understanding the Adapter Hierarchy
In modern Android development,ViewPager2 has largely replaced the older ViewPager. Consequently, the adapter classes have changed significantly. The most common source of this constructor error is attempting to use legacy patterns with new APIs or vice versa. If you are using ViewPager2, your adapter must extend FragmentStateAdapter. This class requires a specific constructor signature that accepts a FragmentActivity as its primary argument.
If you are still using the original ViewPager, you should extend FragmentPagerAdapter or FragmentStatePagerAdapter. These classes require a FragmentManager in their constructor. Mixing these up leads to immediate compilation failures because the constructor signatures do not match what you are providing. For instance, trying to pass a generic Context to a constructor that expects an Activity will fail. It is crucial to identify which ViewPager implementation your project relies on before writing the adapter code.
Correct Constructor Signatures
When extendingFragmentStateAdapter for ViewPager2, the constructor must look like this:
public class ViewPagerAdapter extends FragmentStateAdapter {
public ViewPagerAdapter(@NonNull FragmentActivity activity) {
super(activity);
}
}
Notice that we pass activity to the super constructor. We cannot pass this if we are inside the Activity itself unless we are careful about initialization order, but typically we pass the activity reference directly. If you try to write super(this) inside an Activity, it might compile if the Activity is a subclass of FragmentActivity, but it is often cleaner to just pass the activity instance. However, if you are defining the adapter as an inner class, ensure the outer class is indeed an Activity.
For the legacy ViewPager, the signature is different:
public class LegacyPagerAdapter extends FragmentPagerAdapter {
private final List fragmentList;
public LegacyPagerAdapter(@NonNull FragmentManager fm, int behavior, List fragments) {
super(fm, behavior);
this.fragmentList = fragments;
}
}
Here, the second argument is the behavior flag, such as BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT. If you omit this or pass an integer where an enum is expected, or vice versa, the compiler will reject the constructor call. Always check the exact number of arguments required by the parent class.
Common Implementation Mistakes
One frequent mistake is failing to implement the mandatory abstract methods. BothFragmentStateAdapter and FragmentPagerAdapter require you to override getItem(int position) and getCount(). If you forget these, the class remains abstract, and you cannot instantiate it. The compiler will explicitly tell you that the class is not abstract and does not override abstract method getItem(int).
Another issue arises from incorrect imports. Developers often import androidx.viewpager.widget.ViewPager while intending to use androidx.viewpager2.widget.ViewPager2. These are entirely different libraries with different adapters. Ensure your build.gradle file includes the correct dependency. For ViewPager2, you need:
implementation 'androidx.viewpager2:viewpager2:1.0.0'
Without this library, any reference to FragmentStateAdapter will result in a "cannot find symbol" error, which can be misinterpreted as a constructor issue if you are trying to fallback to old code.
Handling Fragment Lists
You must provide the fragments to the adapter. InFragmentStateAdapter, you typically store a list of fragments or IDs. A common pattern is to pass a list of fragment instances to the adapter's constructor. However, be cautious with object lifecycles. If you create fragments in the Activity and pass them to the adapter, ensure they are properly detached and reattached by the system.
public ViewPagerAdapter(@NonNull FragmentActivity activity, List fragments) {
super(activity);
this.fragments = fragments;
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
In this example, we store the fragments in a member variable. The getItem method returns the fragment at the specified position. This simple structure resolves most constructor-related issues because it adheres strictly to the FragmentStateAdapter contract.
Debugging Steps
If you still encounter errors, follow these diagnostic steps. First, verify the superclass. Right-click on your adapter class name and choose "Go To Superclass". Ensure it is eitherFragmentStateAdapter or FragmentPagerAdapter. Second, check the constructor parameters. Compare them side-by-side with the official documentation for the specific version of AndroidX you are using. Third, clean and rebuild your project. Sometimes IDE caches cause false positives regarding missing symbols.
It is also worth noting that you cannot use addFragments or similar non-standard methods. Stick to the standard lifecycle management provided by the adapter. If you need dynamic updates, modify the underlying list and call notifyDataSetChanged().
Frequently Asked Questions
Why does my adapter say it is not abstract?
This error means you have forgotten to implement the required abstract methods like getItem() and getCount(). You must define these methods in your adapter class to make it concrete and instantiable.
Can I use Context instead of FragmentActivity?
No, FragmentStateAdapter specifically requires a FragmentActivity. Passing a generic Context will cause a compilation error because the adapter needs access to the fragment manager associated with the activity.
What is the difference between FragmentPagerAdapter and FragmentStateAdapter?
FragmentPagerAdapter keeps all created fragments in memory, which is good for a small number of tabs. FragmentStateAdapter destroys fragments that are not visible, saving memory for larger datasets. Use FragmentStateAdapter with ViewPager2.
How do I fix "cannot find symbol" for FragmentStateAdapter?
Ensure you have added the viewpager2 dependency in your build.gradle file and that you are importing from androidx.viewpager2.app.FragmentStateAdapter.
Do I need to manage fragment transactions manually?
No, the adapter handles fragment transactions automatically. You only need to provide the fragments in getItem() and report the count via getCount().