[ActionBar]无标题栏时点击菜单键出现空指针的解决办法.
来源:互联网 发布:刺客信条康纳 知乎 编辑:程序博客网 时间:2024/06/05 08:45
1.当Activity继承ActionBarActivity时,若无标题栏,则会出现空指针异常:
<item name="android:windowNoTitle">true</item>
04-13 18:36:27.984: E/AndroidRuntime(1969): FATAL EXCEPTION: main04-13 18:36:27.984: E/AndroidRuntime(1969): java.lang.NullPointerException04-13 18:36:27.984: E/AndroidRuntime(1969): at android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:287)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.support.v7.app.ActionBarImplJB.getThemedContext(ActionBarImplJB.java:20)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.support.v7.app.ActionBarActivityDelegate.getMenuInflater(ActionBarActivityDelegate.java:98)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.support.v7.app.ActionBarActivity.getMenuInflater(ActionBarActivity.java:71)04-13 18:36:27.984: E/AndroidRuntime(1969): at com.fanhl.flingover.MainActivity.onCreateOptionsMenu(MainActivity.java:108)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.app.Activity.onCreatePanelMenu(Activity.java:2551)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)04-13 18:36:27.984: E/AndroidRuntime(1969): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:465)04-13 18:36:27.984: E/AndroidRuntime(1969): at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:853)04-13 18:36:27.984: E/AndroidRuntime(1969): at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1523)04-13 18:36:27.984: E/AndroidRuntime(1969): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1921)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3659)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3629)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2870)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.os.Handler.dispatchMessage(Handler.java:99)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.os.Looper.loop(Looper.java:137)04-13 18:36:27.984: E/AndroidRuntime(1969): at android.app.ActivityThread.main(ActivityThread.java:4881)04-13 18:36:27.984: E/AndroidRuntime(1969): at java.lang.reflect.Method.invokeNative(Native Method)04-13 18:36:27.984: E/AndroidRuntime(1969): at java.lang.reflect.Method.invoke(Method.java:511)04-13 18:36:27.984: E/AndroidRuntime(1969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:804)04-13 18:36:27.984: E/AndroidRuntime(1969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:571)04-13 18:36:27.984: E/AndroidRuntime(1969): at dalvik.system.NativeStart.main(Native Method)
2.解决办法是.
2.1:禁止点击菜单键
/** * 禁止点击菜单键 */@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_MENU) {// do nothingreturn true;}return super.onKeyDown(keyCode, event);}
2.2:不禁用标题栏
URL: How to hide action bar before activity is created, and then show it again?
Setting android:windowActionBar=false
truly disables the ActionBar but then, as you say, getActionBar()
returns null. This is solved by:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_ACTION_BAR); getActionBar().hide(); setContentView(R.layout.splash); // be sure you call this AFTER requestFeature
This creates the ActionBar and immediately hides it before it had the chance to be displayed.
But now there is another problem. After putting windowActionBar=false
in the theme, the Activity draws its normal Window Title instead of an ActionBar.
If we try to avoid this by using some of the *.NoTitleBar
stock themes or we try to put <item name="android:windowNoTitle">true</item>
in our own theme, it won't work.
The reason is that the ActionBar depends on the Window Title to display itself - that is the ActionBar is a transformed Window Title.
So the trick which can help us out is to add one more thing to our Activity theme xml:
<item name="android:windowActionBar">false</item><item name="android:windowTitleSize">0dp</item>
This will make the Window Title with zero height, thus practically invisible .
In your case, after you are done with displaying the splash screen you can simply call
setContentView(R.layout.main);getActionBar().show()
and you are done. The Activity will start with no ActionBar flickering, nor Window Title showing.
ADDON: If you show and hide the ActionBar multiple times maybe you have noticed that the first showing is not animated. From then on showing and hiding are animated. If you want to have animation on the first showing too you can use this:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_ACTION_BAR); // delaying the hiding of the ActionBar Handler h = new Handler(); h.post(new Runnable() { @Override public void run() { getActionBar().hide(); } });
The same thing can be achieved with:
protected void onPostResume() { super.onPostResume(); getActionBar().hide();
but it may need some extra logic to check if this is the first showing of the Activity.
The idea is to delay a little the hiding of the ActionBar. In a way we let the ActionBar be shown, but then hide it immediately. Thus we go beyond the first non-animated showing and next showing will be considered second, thus it will be animated.
As you may have guessed there is a chance that the ActionBar could be seen before it has been hidden by the delayed operation. This is actually the case. Most of the time nothing is seen but yet, once in a while, you can see the ActionBar flicker for a split second.
In any case this is not a pretty solution, so I welcome any suggestions.
- [ActionBar]无标题栏时点击菜单键出现空指针的解决办法.
- 关于使用AIDL出现空指针的解决办法
- RecyclerView.addFocusables出现空指针异常NullPointerException的解决办法
- 关于使用AIDL出现空指针的解决办法
- 关于使用svn checkout时点击无反应的情况
- ActionBar SearchView空指针异常
- 使用getHibernateTemplate()的方法时出现空指针异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- ssh框架整合时使用hibernateTemplate进行find查询时出现空指针异常的解决办法
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- AndroidStudio 依赖 ButterKnife 出现的空指针异常,无效操作解决办法
- 如何隐藏ActionBar的标题栏
- 如何隐藏ActionBar的标题栏
- Android Studio使用ButterKnife注解 出现空指针异常解决办法
- 使用java命令行方式编译包中的类
- .jar服务器启动。
- MySQL Stored Procedure Variables
- git版本控制从某个版本恢复到之前或者之后的版本的方法
- java图片添加文字
- [ActionBar]无标题栏时点击菜单键出现空指针的解决办法.
- 第6周作业2-IF语句大显身手之成绩判断
- 界面几个例子
- STL应用——神奇的set
- 献丑一个“及时度”算法
- Display layout of linerlayout by %
- 十大高明的Google搜索技巧
- 寻找迷宫出路
- 我的笔记