• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Adapter and ViewPage2 error: incompatible types: ViewPager2 cannot be converted to ViewPager

Hi,

I'm trying to build a hello world app with tabs.
The problem I'm encountering now is that I get some errors, using adapter on ViewPager2.
I updated from ViewPager to ViewPager2.

Here's the code:

app > java > MainActivity.java

Java:
public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private ViewPagerAdapter adapter;
    private TabLayout tabLayout;
    private Spinner spinner2;

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

        //this creates the three-dot button in the right on the toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        //uses toolbar as action bar
        setSupportActionBar(toolbar);

        ViewPager2 viewPager2 = findViewById(R.id.pager);
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager2.setAdapter(adapter); // <- adapter in this line is the problem (error: incompatible types: ViewPagerAdapter cannot be converted to Adapter)

        tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager2); // <- and this variable: viewPager2 (error: incompatible types: ViewPager2 cannot be converted to ViewPager)
    }

app > res > layout > activity_main.xml

Code:
<androidx.viewpager2.widget.ViewPager2
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Dependencies I have:

upload_2019-10-3_14-43-41.png


What's wrong?
Thanks
 
Something similar happened to me, it was caused because I was importing androidx.fragment.app.FragmentStatePagerAdapter instead of androidx.viewpager2.adapter.FragmentStateAdapter
 
Back
Top Bottom