Home > Software design >  App kept stop running in Emulator in Android Studio
App kept stop running in Emulator in Android Studio

Time:08-12

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

Image of my logcat

CodePudding user response:

In the line:

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

The code tries to first look into your activity_main.xml file and find a xml object with the id R.id.viewpagerClipBridge. The xml object is probably of type ViewPager.

In the next line the code looks again into the xml file. Again with the id R.id.viewpagerClipBridge but this time the code tries to set the return value to a variable tabLayout 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.

  • Related