Issue
I've been following a tutorial on YouTube about tab layouts and when I was finish following it and run the application, the app kept stop running. So, I open my logcat to see what was wrong and it says: "Caused by java.lang.ClassCastException: androidx.viewpager.widget.ViewPager cannot be cast to com.google.android.material.tabs.TabLayout". I was unable to find what "cannot be cast to" means and can't find any documents explaining it.
private Toolbar toolbar;
private ViewPager viewPager;
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbarClipBridge);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.viewpagerClipBridge);
tabLayout = findViewById(R.id.viewpagerClipBridge);
fragmentDevices fragmentDevices = new fragmentDevices();
fragmentSyncActivity fragmentSyncActivity = new fragmentSyncActivity();
fragmentSettings fragmentSettings = new fragmentSettings();
ViewPageAdapter viewPageAdapter = new ViewPageAdapter(getSupportFragmentManager(), 0);
viewPageAdapter.addFragment(fragmentDevices, "Devices");
viewPageAdapter.addFragment(fragmentSyncActivity, "Sync Activity");
viewPageAdapter.addFragment(fragmentSettings, "Settings");
viewPager.setAdapter(viewPageAdapter);
tabLayout.getTabAt(0).setIcon(R.drawable.ic_baseline_devices_24);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_baseline_sync_24);
tabLayout.getTabAt(2).setIcon(R.drawable.ic_baseline_settings_24);
}
private class ViewPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments = new ArrayList<>();
private List<String> fragmentTitle = new ArrayList<>();
public ViewPageAdapter(@NonNull FragmentManager fm, int behavior) {
super(fm, behavior);
}
public void addFragment(Fragment fragment, String title){
fragments.add(fragment);
fragmentTitle.add(title);
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitle.get(position);
}
}
Logcat:
Caused by: java.lang.ClassCastException: androidx.viewpager.widget.ViewPager cannot be cast to com.google.android.material.tabs.TabLayout
Solution
In the line:
viewPager = findViewById(R.id.viewpagerClipBridge);
tabLayout = findViewById(R.id.viewpagerClipBridge);
The code tries to look into your activity_main.xml
file and find an XML object with the id R.id.viewpagerClipBridge
. The XML object is probably of the type ViewPager.
In the following line, the code looks again into the XML file. Again, with the id R.id.viewpagerClipBridge
, the code tries to set the return value to a variable tab layout
of type TabLayout
.
And since the type of ViewPager != type of TabLayout it cannot be cast to that type.
I think you used the wrong id in the second findViewById
line. Investigate your activity_main.xml
and look for the id of your TabLayout
.
Answered By - Traendy
Answer Checked By - Gilberto Lyons (JavaFixing Admin)