应用程序崩溃时preSS MENU按钮 [英] App crash when press MENU button

查看:182
本文介绍了应用程序崩溃时preSS MENU按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序使用支持V4库:

 <采用-SDK
    安卓的minSdkVersion =7
    机器人:maxSdkVersion =19
    机器人:targetSdkVersion =19/>

和它只是作为创建一个新的项目。当我在我的手机和preSS菜单按钮运行它,它崩溃了:


  

15 12-21:12:54.170 31705-31705 / com.talkweb.woplus E / AndroidRuntime:致命异常:主要
      显示java.lang.NullPointerException
              在android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:287)
              在android.support.v7.app.ActionBarImplJB.getThemedContext(ActionBarImplJB.java:20)
              在android.support.v7.app.ActionBarActivityDelegate.getMenuInflater(ActionBarActivityDelegate.java:98)
              在android.support.v7.app.ActionBarActivity.getMenuInflater(ActionBarActivity.java:71)
              在com.talkweb.woplus.HomeActivity.onCreateOptionsMenu(HomeActivity.java:35)
              在android.app.Activity.onCreatePanelMenu(Activity.java:2652)


code在HomeActivity.java:

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    requestWindowFeature(Window.FEATURE_NO_TITLE); //我加入这行
    的setContentView(R.layout.activity_home);    如果(savedInstanceState == NULL){
        webViewFragment =新WebViewFragment();
        getSupportFragmentManager()调用BeginTransaction()
                。新增(R.id.container,webViewFragment)
                。承诺();
    }}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.home,菜单); //行:35
    返回true;
}

styles.xml:

 <资源>
    <! - Base应用程序的主题。 - >
    <样式名称=AppTheme父=Theme.AppCompat>
    < /风格>
< /资源>

清单:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.talkweb.woplus>    <使用许可权的android:NAME =android.permission.INTERNET对/>    <用途-SDK
        安卓的minSdkVersion =7
        机器人:maxSdkVersion =19
        机器人:targetSdkVersion =19/>    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:主题=@风格/ AppTheme
        机器人:标签=@字符串/ APP_NAME>        <活动
            机器人:名字=com.talkweb.woplus.HomeActivity
            机器人:标签=@字符串/ APP_NAME>            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>< /清单>

菜单:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    工具:上下文=com.talkweb.woplus.HomeActivity>    <项目机器人:ID =@ + ID / action_settings
        机器人:标题=@字符串/ action_settings
        机器人:orderInCategory =100
        机器人:showAsAction =ifRoom | withText
        应用:showAsAction =ifRoom | withText/>
&所述; /菜单>


解决方案

根据在问题分析<一个href=\"http://stackoverflow.com/questions/19275447/oncreateoptionsmenu-causing-error-in-an-activity-with-no-actionbar\">OnCreateOptionsMenu在没有动作条的活动造成的错误,这是因为操作栏为空。那是因为这样:


  requestWindowFeature(Window.FEATURE_NO_TITLE); //我加入这行


这消除除其他事项外的动作吧。

现在,你的要求是矛盾的:你想有一个菜单,但你不想要一个操作栏。 A从底部弹出菜单是不是你可以有,至少不会使用标准的Andr​​oid组件。你必须之间进行选择:


  1. 动作栏和菜单,删除 FEATURE_NO_TITLE


  2. 没有行动起来吧,没有菜单,保持 FEATURE_NO_TITLE


My android app is using support v4 library:

<uses-sdk
    android:minSdkVersion="7"
    android:maxSdkVersion="19"
    android:targetSdkVersion="19" />

And it is just created as a new project. When I run it on my phone and press the menu button, it crashes:

12-21 15:12:54.170 31705-31705/com.talkweb.woplus E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:287) at android.support.v7.app.ActionBarImplJB.getThemedContext(ActionBarImplJB.java:20) at android.support.v7.app.ActionBarActivityDelegate.getMenuInflater(ActionBarActivityDelegate.java:98) at android.support.v7.app.ActionBarActivity.getMenuInflater(ActionBarActivity.java:71) at com.talkweb.woplus.HomeActivity.onCreateOptionsMenu(HomeActivity.java:35) at android.app.Activity.onCreatePanelMenu(Activity.java:2652)

code in HomeActivity.java:

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

    requestWindowFeature(Window.FEATURE_NO_TITLE);  // I add this line
    setContentView(R.layout.activity_home);

    if (savedInstanceState == null) {
        webViewFragment = new WebViewFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, webViewFragment)
                .commit();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);  // line: 35
    return true;
}

styles.xml:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat">
    </style>
</resources>

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.talkweb.woplus">

    <uses-permission android:name="android.permission.INTERNET" />

    <uses-sdk
        android:minSdkVersion="7"
        android:maxSdkVersion="19"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme"
        android:label="@string/app_name">

        <activity
            android:name="com.talkweb.woplus.HomeActivity"
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.talkweb.woplus.HomeActivity" >

    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        android:showAsAction="ifRoom|withText"
        app:showAsAction="ifRoom|withText" />
</menu>

解决方案

Based on analysis in the question "OnCreateOptionsMenu causing error in an activity with no actionbar", this is because the action bar is null. And that's because of this:

requestWindowFeature(Window.FEATURE_NO_TITLE);  // I add this line

which removes the action bar among other things.

Now, your requirements are conflicting: you want a menu but you don't want an action bar. A "menu popup from bottom" is not something you can have, at least not using the standard Android components. You'll have to choose between:

  1. Action bar and menu, removing the FEATURE_NO_TITLE.

  2. No action bar, no menu, keeping the FEATURE_NO_TITLE.

这篇关于应用程序崩溃时preSS MENU按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆