工具栏问题

时间:2017-06-09 14:57:41

标签: java android

我正在尝试使用本指南制作工具栏https://developer.android.com/training/appbar/actions.html 我的菜单图标没有出现。当我把它给我带充气机的错误。

我的代码main_activity.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.grouper4.MainActivity"
    android:orientation="vertical">

    <include layout="@layout/toolbar"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</LinearLayout>

我的mainactivity.java

toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/colorPrimary">

    <item
        android:id="@+id/item1"
        android:showAsAction="always"
        android:title="TEST">
    </item>
    </android.support.v7.widget.Toolbar>

错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.android.grouper4, PID: 19791
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.grouper4/com.example.android.grouper4.MainActivity}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class item
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
               Caused by: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class item
               Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class item
               Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.item" on path: DexPathList[[zip file "/data/app/com.example.android.grouper4-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.android.grouper4-2/lib/x86, /system/lib, /vendor/lib]]
                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                  at android.view.LayoutInflater.createView(LayoutInflater.java:609)
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:700)
                  at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                  at com.example.android.grouper4.MainActivity.onCreate(MainActivity.java:33)
                  at android.app.Activity.performCreate(Activity.java:6679)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 个答案:

答案 0 :(得分:2)

试试这个,

1&GT;为菜单创建一个新的资源目录,并将其命名为菜单。

make new resource directory

2 - ;为新菜单创建新的布局文件。将其命名为new_menu.xml

3&GT;现在为您的菜单添加一些项目......

4&GT;现在显示新菜单......

[
    {
        "FamilyName": "Smith",
        "Children": [
            {
                "FirstName": "John"
            },
            {
                "FirstName": "Mike"
            }
        ]
    },
    {
        "FamilyName": "Williams",
        "Children": [
            {
                "FirstName": "Mark"
            },
            {
                "FirstName": "Mark"
            },
            {
                "FirstName": "Peter"
            }
        ]
    }
]

5个当点击任何菜单项时......

@Override         
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.new_menu, menu);
    return super.onCreateOptionsMenu(menu);
}