1

我正在尝试创建一个menu项目。当我运行我的应用程序时,它在启动时立即崩溃,并且在 LogCat 中出现以下错误:

E/AndroidRuntime(1507):原因:java.lang.ClassNotFoundException:在路径:/data/app/com.thing.appname-2.apk 上找不到类“android.view.menu”

这是我的 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>

以下是onCreate方法之外的(不知道有没有区别):

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);
    }

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

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

4 回答 4

4

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.

于 2013-05-19T19:19:42.013 回答
0

I ran into a similar problem, the app crashed and I got runtime error caused by:

java.lang.ClassNotFoundException: Didn't find class "android.view.style"

Removing the 'style' items from the layout file (activity_main.xml) solved it. I assume that if the style items had been needed, a proper import would have solved the problem.

于 2016-11-18T13:02:46.907 回答
0

当我的布局文件(activity_main.xml)有错误的“样式”项目时,我遇到了同样的问题。

从 xml 文件中删除错误的项目解决了它。

于 2016-11-18T12:48:53.443 回答
-1

您可能正在使用 ActionbarSherlock。如果是这种情况,请尝试导入

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

代替android.view.menu

于 2013-05-18T18:29:28.227 回答