Android隐藏ActionBar

时间:2012-10-06 10:19:33

标签: android android-actionbar

我正试图隐藏我的操作栏。请检查我的代码。它似乎没有用。我还在我的代码底部列出了我的LOGCAT。请帮忙。

MainActivity.java

public class MainActivity extends SherlockFragmentActivity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            super.onCreate(savedInstanceState);

            ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            actionBar.setDisplayShowTitleEnabled(true);

            Tab tab = actionBar.newTab()
                                .setText("Android")
                                .setTabListener(new AndroidFragment())
                                .setIcon(R.drawable.android);

            actionBar.addTab(tab);

            tab = actionBar.newTab()
                    .setText("Apple")
                    .setTabListener(new AppleFragment())
                    .setIcon(R.drawable.apple);

            actionBar.addTab(tab);


        }  

    }

LOGCAT:

10-06 18:17:25.781: E/AndroidRuntime(15962): FATAL EXCEPTION: main
10-06 18:17:25.781: E/AndroidRuntime(15962): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.actionbarsherlocknavtab/in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity}: java.lang.NullPointerException
10-06 18:17:25.781: E/AndroidRuntime(15962):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at android.os.Looper.loop(Looper.java:137)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at android.app.ActivityThread.main(ActivityThread.java:4512)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at java.lang.reflect.Method.invokeNative(Native Method)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at java.lang.reflect.Method.invoke(Method.java:511)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at dalvik.system.NativeStart.main(Native Method)
10-06 18:17:25.781: E/AndroidRuntime(15962): Caused by: java.lang.NullPointerException
10-06 18:17:25.781: E/AndroidRuntime(15962):    at in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity.onCreate(MainActivity.java:19)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at android.app.Activity.performCreate(Activity.java:4465)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
10-06 18:17:25.781: E/AndroidRuntime(15962):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
10-06 18:17:25.781: E/AndroidRuntime(15962):    ... 11 more

1 个答案:

答案 0 :(得分:9)

好吧,如果你使用requestWindowFeature(Window.FEATURE_NO_TITLE); ActionBar对象,你读后面的三行是null。因此NPE。

但为什么要以这种方式隐藏ActionBar 然后获取对象并设置标签以及诸如隐形ActionBar之类的东西?

另见安德烈对你问题的评论。