如何删除"片段活动中的标题栏"?

时间:2016-07-13 07:57:54

标签: android android-fragments android-activity android-actionbar

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // 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) {
        }
    });
}

那是我的代码。如果我使用" requestWindowFeature(Window.FEATURE_NO_TITLE);",则返回NullPointerException。请帮忙!我只想删除标题栏而不是片段活动中的操作栏。

我收到以下错误:



07-13 04:06:44.623 8610-8610/com.example.swipeviewtest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.swipeviewtest, PID: 8610
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.swipeviewtest/com.example.swipeviewtest.MainActivity}: java.lang.NullPointerException
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                             at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
                                                                             at android.app.ActivityThread.access$900(ActivityThread.java:135)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:136)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:515)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                          Caused by: java.lang.NullPointerException
                                                                             at com.example.swipeviewtest.MainActivity$override.onCreate(MainActivity.java:34)
                                                                             at com.example.swipeviewtest.MainActivity$override.access$dispatch(MainActivity.java)
                                                                             at com.example.swipeviewtest.MainActivity.onCreate(MainActivity.java:0)
                                                                             at android.app.Activity.performCreate(Activity.java:5231)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                             at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738) 
                                                                             at android.app.ActivityThread.access$900(ActivityThread.java:135) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:136) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                             at dalvik.system.NativeStart.main(Native Method) 




2 个答案:

答案 0 :(得分:0)

使用这种方式..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);

答案 1 :(得分:0)

<style name="NoActionBar" parent="Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
</style>

在你的Manifest定义中......它会起作用

<activity
        android:name=".MyActivity"
        android:theme="@style/NoActionBar"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>