[英]Why am I getting this Exception when I try to remove Action Bar on my Activity?
我正在尝试在下面的LoginActivity上删除ActionBar。 我已经从该示例代码中实现了代码,但是我不希望显示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)
把它放在你的styles.xml中
<item name="android:windowActionBar">false</item>
并扩展Activity
类而不是AppCompatActivity
出现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>
不要扩展AppCompatActivity。 另外,进入您的xml布局文件并删除操作栏/工具栏
卸下操作栏
您可以在运行时通过调用hide()隐藏操作栏。
例如:
ActionBar actionBar = getActionBar();
actionBar.hide();
API级别11或更高
使用getActionBar()方法获取ActionBar 。
代替:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.