【问题标题】:Didn't find class "android.view.menu"没有找到类“android.view.menu”
【发布时间】:2020-02-17 00:07:39
【问题描述】:

我遇到了以下问题:

原因:android.view.InflateException: Binary XML file line #13: Binary XML file line #2: Error inflating class menu 引起:android.view.InflateException:二进制 XML 文件第 2 行:膨胀类菜单时出错 引起:java.lang.ClassNotFoundException:在路径上找不到类“android.view.menu”:DexPathList [[zip文件“/data/app/com.cpsc4150.glovebox-xjT3LiC43G5FDyD3dDmxuw==/base.apk”] ,nativeLibraryDirectories=[/data/app/com.cpsc4150.glovebox-xjT3LiC43G5FDyD3dDmxuw==/lib/x86, /system/lib]]

我尝试过清理构建、删除构建文件并让 android studio 重新制作它们等。没有 f 似乎有效。

我的菜单文件在 res/menu/menu.xml 中,并且是唯一位于那里的东西。

主活动

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


    /**
     * <p>Defines the actions to be taken when the menu activity is created</p>
     * @param savedInstanceState a saved instance of the menu activity id one exist
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        int REQUEST_ALL = 1;
        String[] PERMISSIONS = {
                android.Manifest.permission.WAKE_LOCK,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.CAMERA
        };

        if (hasPermissions(this, PERMISSIONS) == false) {
            ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_ALL);
        }

        serviceList = loadServices(SERVICE_LIST_ID);
        if (serviceList == null) serviceList = new ArrayList<>();
        inProgressList = loadServices(IN_PROGRESS_LIST_ID);
        if (inProgressList == null) inProgressList = new ArrayList<>();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.my_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();

        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setOnTabSelectedListener(tabListener);

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_toolbar"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:layout_height="?attr/actionBarSize"
    xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<!--        <item android:id="@+id/privacyPolicy"-->
<!--            android:title="Privacy Policy"-->
<!--            android:icon="@drawable/ic_more_vert_black_24dp"-->
<!--            app:showAsAction="always"></item>-->

</menu>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:backgroundTint="@color/appBackground"
    android:background="@color/appBackground"


    >
<include
    layout="@menu/menu"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"/>

【问题讨论】:

    标签: java android xml android-studio android-layout


    【解决方案1】:

    删除:

    <include
        layout="@menu/menu"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>
    

    &lt;include&gt; 仅用于包含布局资源,不包含菜单资源。

    【讨论】:

    • 这确实解决了原来的问题,但是现在我的菜单根本不显示?
    • @Boboyobo:您需要使用菜单资源来填充操作栏 Toolbar 或类似的东西。
    猜你喜欢
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多