ViewPager无法在某些Android设备上运行

时间:2015-11-20 09:08:51

标签: android tabs android-viewpager

我正在使用ViewPager和Tabpageradapter将一个活动刷到另一个活动但我在android 4.0版本下面找不到类异常。

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" />

在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)

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您可能需要查看thisthis。 ViewPager的最低版本是4.0。为了兼容性,您可以使用链接中的库。第二个链接为您提供了兼容性库。

答案 2 :(得分:0)

除了Sohaib的回答,指出您应该使用兼容性库,如果要从Web服务加载内容,则可能需要从FragmentStatePagerAdapter扩展。我之前遇到过类似的问题,只是更改基本的PagerAdapter解决了这个问题。

答案 3 :(得分:0)

仔细查看您的onCreate方法。

在我的情况下,如果未授予权限,我正在检查某些权限并使用return;

这就是为什么我无法在棉花糖上运行我的应用程序,但可以在较低版本上运行我的应用程序。