2

我试图隐藏我的操作栏。请检查我的代码。它似乎不起作用。我还在代码底部列出了我的 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);


        }  

    }

日志猫:

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
4

1 回答 1

9

好吧,如果您使用后面三行读取requestWindowFeature(Window.FEATURE_NO_TITLE);的对象,则为空。ActionBar因此,NPE。

但是为什么要以这种方式隐藏ActionBar,然后获取对象并将选项卡设置为不可见的ActionBar?

另请参阅 Andrei 对您的问题的评论。

于 2012-10-06T22:18:41.590 回答