在操作栏选项卡的选项卡侦听器的onTabSelected方法中,PageViewer的空指针异常

时间:2014-10-18 20:39:10

标签: android android-actionbar android-tabs

package com.example.swipetabs;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Window;

@SuppressLint("NewApi") public class MainActivity extends FragmentActivity implements TabListener {
    ViewPager page;
    @SuppressLint("NewApi") @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_main);
        final ActionBar actionBar=getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        Tab tab1=actionBar.newTab();
        Tab tab2=actionBar.newTab();
        Tab tab3=actionBar.newTab();
        tab1.setTabListener(this);
        tab2.setTabListener(this);
        tab3.setTabListener(this);
        tab1.setText("sparta1");
        tab2.setText("sparta2");
        tab3.setText("sparta3");
        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);
        page=new ViewPager(this);
        page=(ViewPager)findViewById(R.id.pager);
        FragmentManager manager=getSupportFragmentManager();
        MyFragmentStatePagerAdapter pagerAdapter=new MyFragmentStatePagerAdapter(manager);
        page.setAdapter(pagerAdapter);
        page.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                actionBar.setSelectedNavigationItem(arg0);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        //page.setCurrentItem(2);


    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        page.setCurrentItem(tab.getPosition());

    }

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

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
    class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter{

        public MyFragmentStatePagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int arg0) {
            Fragment fragment=null;
            if(arg0==0){
              fragment=new FragmentA();
            }
            if(arg0==1){
                fragment=new FragmentB();
            }
            if(arg0==2){
                fragment=new FragmentC();
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }


    }
}

public void onTabSelected(Tab tab, FragmentTransaction ft) {
        page.setCurrentItem(tab.getPosition());

    }

上面的page.setCurrentItem(tab.getPosition())返回null异常。我无法弄清楚问题。我希望有人能够弄明白。 谢谢 这是错误日志

10-18 16:04:15.496: D/AndroidRuntime(1794): Shutting down VM
10-18 16:04:15.496: W/dalvikvm(1794): threadid=1: thread exiting with uncaught exception (group=0x41c18ba8)
10-18 16:04:15.496: E/AndroidRuntime(1794): FATAL EXCEPTION: main
10-18 16:04:15.496: E/AndroidRuntime(1794): Process: com.example.swipetabs, PID: 1794
10-18 16:04:15.496: E/AndroidRuntime(1794): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.swipetabs/com.example.swipetabs.MainActivity}: java.lang.NullPointerException
10-18 16:04:15.496: E/AndroidRuntime(1794):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at android.os.Looper.loop(Looper.java:136)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at android.app.ActivityThread.main(ActivityThread.java:5001)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at java.lang.reflect.Method.invokeNative(Native Method)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at java.lang.reflect.Method.invoke(Method.java:515)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at dalvik.system.NativeStart.main(Native Method)
10-18 16:04:15.496: E/AndroidRuntime(1794): Caused by: java.lang.NullPointerException
10-18 16:04:15.496: E/AndroidRuntime(1794):     at com.example.swipetabs.MainActivity.onTabSelected(MainActivity.java:68)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:506)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:492)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at com.example.swipetabs.MainActivity.onCreate(MainActivity.java:33)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at android.app.Activity.performCreate(Activity.java:5231)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-18 16:04:15.496: E/AndroidRuntime(1794):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-18 16:04:15.496: E/AndroidRuntime(1794):     ... 11 more
10-18 16:05:24.880: I/Adreno-EGL(1966): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
10-18 16:05:24.931: D/OpenGLRenderer(1966): Enabling debug mode 0
10-18 16:08:35.814: D/dalvikvm(1966): GC_FOR_ALLOC freed 207K, 3% free 9291K/9528K, paused 16ms, total 16ms
10-18 16:08:47.997: I/Adreno-EGL(2310): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
10-18 16:08:48.037: D/OpenGLRenderer(2310): Enabling debug mode 0
10-18 16:09:41.394: D/AndroidRuntime(2457): Shutting down VM
10-18 16:09:41.394: W/dalvikvm(2457): threadid=1: thread exiting with uncaught exception (group=0x41c18ba8)
10-18 16:09:41.394: E/AndroidRuntime(2457): FATAL EXCEPTION: main
10-18 16:09:41.394: E/AndroidRuntime(2457): Process: com.example.swipetabs, PID: 2457
10-18 16:09:41.394: E/AndroidRuntime(2457): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.swipetabs/com.example.swipetabs.MainActivity}: java.lang.NullPointerException
10-18 16:09:41.394: E/AndroidRuntime(2457):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at android.os.Looper.loop(Looper.java:136)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at android.app.ActivityThread.main(ActivityThread.java:5001)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at java.lang.reflect.Method.invokeNative(Native Method)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at java.lang.reflect.Method.invoke(Method.java:515)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at dalvik.system.NativeStart.main(Native Method)
10-18 16:09:41.394: E/AndroidRuntime(2457): Caused by: java.lang.NullPointerException
10-18 16:09:41.394: E/AndroidRuntime(2457):     at com.example.swipetabs.MainActivity.onTabSelected(MainActivity.java:66)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:506)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:492)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at com.example.swipetabs.MainActivity.onCreate(MainActivity.java:33)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at android.app.Activity.performCreate(Activity.java:5231)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-18 16:09:41.394: E/AndroidRuntime(2457):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-18 16:09:41.394: E/AndroidRuntime(2457):     ... 11 more
10-18 16:11:01.960: I/Adreno-EGL(2680): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
10-18 16:11:01.990: D/OpenGLRenderer(2680): Enabling debug mode 0
10-18 16:12:30.865: D/AndroidRuntime(3006): Shutting down VM
10-18 16:12:30.865: W/dalvikvm(3006): threadid=1: thread exiting with uncaught exception (group=0x41c18ba8)
10-18 16:12:30.865: E/AndroidRuntime(3006): FATAL EXCEPTION: main
10-18 16:12:30.865: E/AndroidRuntime(3006): Process: com.example.swipetabs, PID: 3006
10-18 16:12:30.865: E/AndroidRuntime(3006): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.swipetabs/com.example.swipetabs.MainActivity}: java.lang.NullPointerException
10-18 16:12:30.865: E/AndroidRuntime(3006):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at android.os.Looper.loop(Looper.java:136)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at android.app.ActivityThread.main(ActivityThread.java:5001)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at java.lang.reflect.Method.invokeNative(Native Method)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at java.lang.reflect.Method.invoke(Method.java:515)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at dalvik.system.NativeStart.main(Native Method)
10-18 16:12:30.865: E/AndroidRuntime(3006): Caused by: java.lang.NullPointerException
10-18 16:12:30.865: E/AndroidRuntime(3006):     at com.example.swipetabs.MainActivity.onTabSelected(MainActivity.java:67)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:506)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:492)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at com.example.swipetabs.MainActivity.onCreate(MainActivity.java:33)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at android.app.Activity.performCreate(Activity.java:5231)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-18 16:12:30.865: E/AndroidRuntime(3006):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-18 16:12:30.865: E/AndroidRuntime(3006):     ... 11 more
10-18 16:21:28.038: D/AndroidRuntime(3720): Shutting down VM
10-18 16:21:28.038: W/dalvikvm(3720): threadid=1: thread exiting with uncaught exception (group=0x41c18ba8)
10-18 16:21:28.038: E/AndroidRuntime(3720): FATAL EXCEPTION: main
10-18 16:21:28.038: E/AndroidRuntime(3720): Process: com.example.swipetabs, PID: 3720
10-18 16:21:28.038: E/AndroidRuntime(3720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.swipetabs/com.example.swipetabs.MainActivity}: java.lang.NullPointerException
10-18 16:21:28.038: E/AndroidRuntime(3720):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at android.os.Looper.loop(Looper.java:136)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at android.app.ActivityThread.main(ActivityThread.java:5001)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at java.lang.reflect.Method.invokeNative(Native Method)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at java.lang.reflect.Method.invoke(Method.java:515)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at dalvik.system.NativeStart.main(Native Method)
10-18 16:21:28.038: E/AndroidRuntime(3720): Caused by: java.lang.NullPointerException
10-18 16:21:28.038: E/AndroidRuntime(3720):     at com.example.swipetabs.MainActivity.onTabSelected(MainActivity.java:67)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:506)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:492)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at com.example.swipetabs.MainActivity.onCreate(MainActivity.java:33)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at android.app.Activity.performCreate(Activity.java:5231)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-18 16:21:28.038: E/AndroidRuntime(3720):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-18 16:21:28.038: E/AndroidRuntime(3720):     ... 11 more
10-18 16:24:39.042: I/Adreno-EGL(3964): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
10-18 16:24:39.072: D/OpenGLRenderer(3964): Enabling debug mode 0
10-18 16:24:49.854: D/dalvikvm(3964): GC_FOR_ALLOC freed 211K, 3% free 9287K/9528K, paused 17ms, total 17ms
10-18 16:29:09.851: I/Adreno-EGL(4899): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
10-18 16:29:09.881: D/OpenGLRenderer(4899): Enabling debug mode 0
10-18 16:37:17.882: D/AndroidRuntime(5785): Shutting down VM
10-18 16:37:17.882: W/dalvikvm(5785): threadid=1: thread exiting with uncaught exception (group=0x41c18ba8)
10-18 16:37:17.882: E/AndroidRuntime(5785): FATAL EXCEPTION: main
10-18 16:37:17.882: E/AndroidRuntime(5785): Process: com.example.swipetabs, PID: 5785
10-18 16:37:17.882: E/AndroidRuntime(5785): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.swipetabs/com.example.swipetabs.MainActivity}: java.lang.NullPointerException
10-18 16:37:17.882: E/AndroidRuntime(5785):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at android.os.Looper.loop(Looper.java:136)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at android.app.ActivityThread.main(ActivityThread.java:5001)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at java.lang.reflect.Method.invokeNative(Native Method)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at java.lang.reflect.Method.invoke(Method.java:515)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at dalvik.system.NativeStart.main(Native Method)
10-18 16:37:17.882: E/AndroidRuntime(5785): Caused by: java.lang.NullPointerException
10-18 16:37:17.882: E/AndroidRuntime(5785):     at com.example.swipetabs.MainActivity.onTabSelected(MainActivity.java:71)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:506)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:492)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at com.example.swipetabs.MainActivity.onCreate(MainActivity.java:36)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at android.app.Activity.performCreate(Activity.java:5231)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-18 16:37:17.882: E/AndroidRuntime(5785):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-18 16:37:17.882: E/AndroidRuntime(5785):     ... 11 more

提前致谢:)

0 个答案:

没有答案