错误:没有找到类android.view.menu(上路径) [英] Error: Didn't find class android.view.menu (on path)

查看:435
本文介绍了错误:没有找到类android.view.menu(上路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个菜单项。当我运行我的应用程序,它崩溃权当它启动起来,我得到了LogCat中出现以下错误:


  

E / AndroidRuntime(1507):因:
  抛出java.lang.ClassNotFoundException:找不到类
  android.view.menu的路径:
  /data/app/com.thing.appname-2.apk


下面是我的XML:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
        <项目机器人:ID =@ + ID / addEventMenu
              机器人:标题=添加事件
              机器人:图标=@绘制/ addeventimage/>
    &所述; /菜单>

以下是onCreate方法之外(不知道这是否有差别):

 公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
            案例R.id.addEventMenu:
                //做一些事情时,这里的菜单按钮为pressed
                返回true;
            默认:
                返回super.onOptionsItemSelected(项目);
        }
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        menu.add(R.id.addEventMenu);
        返回super.onCreateOptionsMenu(菜单);
    }

我也试过这个,我也得到了同样的错误:

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    MenuInflater吹气= getMenuInflater();
    inflater.inflate(R.menu.main,菜单);
    返回true;
}


解决方案

我遇到了同样的问题前,当我开始Android的发展...

有在你的项目资源下的菜单不同的XML文件 - 这就是太大区别从布局XML文件。把<菜单> <项目> (S)在RES /菜单/ main.xml中

此外,在Android Studio有告诉你要导入的东西一种奇怪的方式......请确保您使用

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    MenuInflater吹气= getMenuInflater();
    inflater.inflate(R.menu.main,菜单);
    返回true;
}

和上冲通知单击导入。

I'm trying to create a single menu item. When I run my app, it crashes right when it starts up and I get the following error in LogCat:

E/AndroidRuntime(1507): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.menu" on path: /data/app/com.thing.appname-2.apk

Here is my XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/addEventMenu"
              android:title="Add Event"
              android:icon="@drawable/addeventimage"/>
    </menu>

The following is outside of the onCreate method (don't know if it makes a difference):

public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.addEventMenu:
                //do something here when menu button is pressed
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(R.id.addEventMenu);
        return super.onCreateOptionsMenu(menu);
    }

I've also tried this and I get the same error:

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

解决方案

I ran into the same problem before when I started Android Development...

There is a different XML file under "menu" in your project resources - this is much different from the layout XML file. Put the <menu> and <item>(s) in the "res/menu/main.xml".

Also, the Android Studio has an odd way of telling you to import stuff... make sure you use

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

and click on the red notification to import.

这篇关于错误:没有找到类android.view.menu(上路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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