当我尝试删除Activity上的Action Bar时,为什么会出现此异常?

时间:2015-06-27 17:51:48

标签: java android android-layout

我正在尝试删除我的LoginActivity上的ActionBar。我已经实现了this示例代码中的代码,但我不想显示ActionBar。我没有改变给出的代码,但我真的找不到如何删除ActionBar(如果可能的话,如果不是只是隐藏它)。

public class LoginActivity extends AppCompatActivity {

// Declaring View and Variables
ViewPager pager;
JoinLoginAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Sign In","Register"};
int Numboftabs = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getSupportActionBar().hide();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.join_login);

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new JoinLoginAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);

}

}

我尝试将NoTheme样式应用于活动,但这给了我以下例外:

06-28 01:43:45.615  11537-11537/com.nauv.jambomall E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nauv.jambomall/com.nauv.jambomall.ui.activity.JoinLoginActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5414)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:320)
        at com.nauv.jambomall.ui.activity.JoinLoginActivity.onCreate(JoinLoginActivity.java:28)
        at android.app.Activity.performCreate(Activity.java:5369)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5414) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)

5 个答案:

答案 0 :(得分:2)

将此内容放入 styles.xml

<item name="android:windowActionBar">false</item>

并扩展Activity类而不是AppCompatActivity

答案 1 :(得分:1)

您获得 AndroidUtilRunTime 的原因是 super.onCreate(savedInstanceState)之后您不应该调用 requestFeature() >。正确的方法是

protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
}

其他删除 ActionBar

的方法
ActionBar actionBar = getActionBar();
actionBar.hide();

如果您使用 android.support.v7.app.ActionBar ,那么

ActionBar actionBar = getSupportActionBar();
actionBar.hide();

另一种方法是在 styles.xml

<item name="android:windowActionBar">false</item>

答案 2 :(得分:1)

不要扩展AppCompatActivity。也可以进入xml布局文件并删除Actionbar / Toolbar

答案 3 :(得分:1)

删除操作栏

您可以通过调用 hide()在运行时隐藏操作栏 例如:

  ActionBar actionBar = getActionBar();  
  actionBar.hide();

在API级别11或更高级上 使用getActionBar()方法获取 ActionBar

答案 4 :(得分:1)

而不是:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getSupportActionBar().hide();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.join_login);

尝试这样的事情:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setContentView(R.layout.join_login);