android:应用程序在工具栏溢出菜单上崩溃点击

时间:2015-05-29 05:52:22

标签: android appcompat-v7-r21

我正在为我的应用程序实现材料设计。我用新的android工具栏替换了原生的ActionBar。添加了菜单项,onOptionsItemSelected完美运行。但是,当我单击溢出图标或具有多个菜单项的项目时,应用程序崩溃。

工具栏截图:https://www.dropbox.com/s/smay4k1qwkd8amz/Screenshot_2015-05-29-11-31-55.png?dl=0

activity_main.xml

    ?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <android.support.v7.widget.Toolbar
            android:id="@+id/app_bar"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:background="@color/my_primary"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    </android.support.v7.widget.Toolbar>

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity"
            />
</LinearLayout>

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="AppTheme.Base">

    </style>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/my_primary</item>
        <item name="colorPrimaryDark">@color/my_primary_dark</item>
        <item name="colorAccent">@color/my_accent</item>
    </style>

</resources>

MyActivity.java

public class MyActivity extends ActionBarActivity {
    private Toolbar toolbar;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

菜单\ main.xml中

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:Compat="http://schemas.android.com/apk/res/com.example.myapp">

    <item
            android:id="@+id/action_add"
            android:title="Add"
            android:icon="@android:drawable/ic_menu_add"
            Compat:showAsAction="always"/>

    <item
            android:title="Items"
            android:icon="@android:drawable/ic_menu_agenda"
            Compat:showAsAction="always"
            >
        <menu>
            <item
                    android:id="@+id/action_1"
                    android:title="item1"/>
            <item
                    android:id="@+id/action_2"
                    android:title="item2"/>
        </menu>
    </item>

    <item
            android:id="@+id/action_about"
            android:title="About"
            android:icon="@android:drawable/ic_menu_help"
            Compat:showAsAction="never"/>

</menu>

崩溃后显示以下消息:

05-29 11:21:18.012  31469-31469/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.myapp, PID: 31469
    java.lang.NoSuchFieldError: No static field title of type I in class Landroid/support/v7/appcompat/R$id; or its superclasses (declaration of 'android.support.v7.appcompat.R$id' appears in /data/app/com.example.myapp-1/base.apk)
            at android.support.v7.internal.view.menu.ListMenuItemView.onFinishInflate(ListMenuItemView.java:89)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:814)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:370)
            at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:219)
            at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:153)
            at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:752)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5293)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

此外,我使用的是最新版本的appcompat库。我跟着教程,谷歌搜索,但没有找到任何东西。这次事故的原因是什么?请帮忙。

2 个答案:

答案 0 :(得分:0)

请在此处阅读.. http://developer.android.com/guide/topics/ui/menus.html 您刚刚创建了菜单,但尚未提供其功能。换句话说,你只是在屏幕上夸大菜单,没有提供功能,如果使用点击它们将会做什么。 要启用它们,您需要实现以下方法,例如

</head>
<body>
    <div>
        <div id="form-container">
            <!--<input type="submit" id="searchsubmit" value="" />-->
            <a class="search-submit-button" href="javascript:void(0)">
                <i class="fa fa-search"></i>
            </a>
            <div id="searchtext">
                <input type="text" id="s" name="s" value="Search Something...">
            </div>
        </div>
    </div>
</body>
中的

答案 1 :(得分:0)

如果您使用trasitive = false;在build.gradle中然后删除它