我试图实现Tab Activity但是我在log cat文件中得到了如下所示的异常

时间:2015-05-06 16:36:05

标签: android

我制作了5个类文件。 3个片段活动1个主要活动和1个适配器类。

logcat的

 05-06 12:17:17.213    1398-1398/com.example.neeraj.tabac E/AndroidRuntime﹕ 
    FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity       ComponentInfo{com.example.neeraj.tabac/com.example.neeraj.tabac.ActivityMain}:                      java.lang.NullPointerException
             android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
           android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            android.app.ActivityThread.access$600(ActivityThread.java:141)
             android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
             android.os.Handler.dispatchMessage(Handler.java:99)
             android.os.Looper.loop(Looper.java:137)
             android.app.ActivityThread.main(ActivityThread.java:5103)
             java.lang.reflect.Method.invokeNative(Native Method)
             java.lang.reflect.Method.invoke(Method.java:525)
             com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
             com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
             dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
             com.example.neeraj.tabac.ActivityMain.onCreate(ActivityMain.java:60)
             android.app.Activity.performCreate(Activity.java:5133)
             android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
             android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
             android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
             android.app.ActivityThread.access$600(ActivityThread.java:141)
             android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
             android.os.Handler.dispatchMessage(Handler.java:99)
             android.os.Looper.loop(Looper.java:137)
             android.app.ActivityThread.main(ActivityThread.java:5103)
             java.lang.reflect.Method.invokeNative(Native Method)
             java.lang.reflect.Method.invoke(Method.java:525)
             com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
             com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
             dalvik.system.NativeStart.main(Native Method)

这是主要的活动文件:

 public class ActivityMain extends FragmentActivity {
        ViewPager Tab;
        TabFragment TabAdapter;
        ActionBar actionBar;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_main);

        TabAdapter = new TabFragment(getSupportFragmentManager());

        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {

                        actionBar = getActionBar();
                        actionBar.setSelectedNavigationItem(position);                    }
                });
        Tab.setAdapter(TabAdapter);

        actionBar = getActionBar();
        //Enable Tabs on Action Bar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){

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

            }

            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                Tab.setCurrentItem(tab.getPosition());
            }

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

            }};
        //Add New Tab
        actionBar.addTab(actionBar.newTab().setText("Android").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("iOS").setTabListener(tabListener));
            actionBar.addTab(actionBar.newTab().setText("Windows").setTabListener(tabListener));

    }
    }

请帮助我作为我的项目,我是Android Studio的新手。即使是罢工也是主要活动,我不知道为什么。

2 个答案:

答案 0 :(得分:0)

看起来你的ActionBar是null,尝试使用。当您使用AppCompat

时会发生这种情况
actionBar = getSupportActionBar();

在定义时,您还需要使用android.support.v7.app.ActionBar actionBar

答案 1 :(得分:0)

或者您可以尝试在setContentView

之前添加此行
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);