简体   繁体   中英

ViewPager not working on some devices Android

I am using ViewPager and Tabpageradapter for swiping one activity to another but I am getting class not found exception below android 4.0 version.

MainActivity****:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    // Initilization Viewpager in main activity
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    context = this.getApplicationContext();
    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        
    actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.blackbox));
    actionBar.setStackedBackgroundDrawable(getResources().getDrawable(
            R.drawable.blackbox));

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

TabsPagerAdapter.class :

//Class using for tabs 
public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Top Rated fragment activity that is the first activity to open
        return new PunjabiFragment();
    case 1:
        // Games fragment activity(Second activity to open
        return new HindiFragment();
    case 2:
        // Movies fragment activity(third activity to open
        return new EnglishFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

AndroidManifest.xml

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="23" />

Getting this error in logcat :

Failed resolving Lcom/Neha/awesomestatus/MainActivity; interface 16 'Landroid/app/ActionBar$TabListener;'
W/dalvikvm(837): Link of class 'Lcom/Neha/awesomestatus/MainActivity;' failed
D/AndroidRuntime(837): Shutting down VM
W/dalvikvm(837): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(837): FATAL EXCEPTION: main
E/AndroidRuntime(837): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.Neha.awesomestatus/com.Neha.awesomestatus.MainActivity}:        java.lang.ClassNotFoundException: com.Neha.awesomestatus.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.Neha.awesomestatus-1.apk]
E/AndroidRuntime(837):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.Neha.awesomestatus.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.Neha.awesomestatus-1.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)

您是否在XML文件中使用<android.support.v4.view.ViewPager />而不是<ViewPager />

You might want to have a look at this and this . Min version for ViewPager is 4.0. For compatibility you can use the library in the link. The second link gives you the compatibility library.

Besides Sohaib's answer, pointing that you should use the compatibility library, if you are loading content from a web service, you may need to extend from FragmentStatePagerAdapter . I had some similar problem before and just changing the base PagerAdapter fixed the issue.

Carefully look at your onCreate method.

In my case I was checking for some permissions and using return; if the permission is not granted.

That is why I could not run my app on the marshmallow, but could run my app on the lower versions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM