当我尝试在 onCreateOptionsMenu 方法中对菜单进行充气时,它似乎会导致 Resources $ NotFoundException ,即使R.java生成了它并且它位于res /菜单文件夹。以下是导致异常的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
我尝试充气的XML文件(res / menu / mainmenu):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:actionLayout="@string/filter_menuitem"
android:showAsAction="ifRoom"
android:title="@string/filter_menuitem"
/>
LogCat的红色输出
10-31 13:42:06.500: E/AndroidRuntime(11629): FATAL EXCEPTION: main
10-31 13:42:06.500: E/AndroidRuntime(11629):android.content.res.Resources$NotFoundException: File Filter from xml type layout resource ID #0x7f090012
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2190)
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2145)
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.content.res.Resources.getLayout(Resources.java:872)
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.android.internal.view.menu.MenuItemImpl.setActionView(MenuItemImpl.java:566)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.actionbarsherlock.internal.view.menu.MenuItemWrapper.setActionView(MenuItemWrapper.java:230)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.actionbarsherlock.view.MenuInflater$MenuState.setItem(MenuInflater.java:454)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.actionbarsherlock.view.MenuInflater$MenuState.addItem(MenuInflater.java:468)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:190)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:112)
10-31 13:42:06.500: E/AndroidRuntime(11629): at edu.calpoly.android.lab3.AdvancedJokeList.onCreateOptionsMenu(AdvancedJokeList.java:112)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.actionbarsherlock.app.SherlockActivity.onCreatePanelMenu(SherlockActivity.java:184)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:560)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:64)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.actionbarsherlock.app.SherlockActivity.onCreateOptionsMenu(SherlockActivity.java:149)
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.app.Activity.onCreatePanelMenu(Activity.java:2449)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:418)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:769)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:3015)
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.os.Handler.handleCallback(Handler.java:605)
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.os.Handler.dispatchMessage(Handler.java:92)
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.os.Looper.loop(Looper.java:137)
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.app.ActivityThread.main(ActivityThread.java:4519)
10-31 13:42:06.500: E/AndroidRuntime(11629): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 13:42:06.500: E/AndroidRuntime(11629): at java.lang.reflect.Method.invoke(Method.java:511)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:995)
10-31 13:42:06.500: E/AndroidRuntime(11629): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
10-31 13:42:06.500: E/AndroidRuntime(11629): at dalvik.system.NativeStart.main(Native Method)
10-31 13:42:06.500: E/AndroidRuntime(11629): Caused by: java.io.FileNotFoundException: Filter
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.content.res.AssetManager.openXmlAssetNative(Native Method)
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:487)
10-31 13:42:06.500: E/AndroidRuntime(11629): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2172)
10-31 13:42:06.500: E/AndroidRuntime(11629): ... 27 more
我的导入语句,如果有帮助
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import com.actionbarsherlock.ActionBarSherlock;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
有关为何抛出此异常的任何反馈?