这是我的menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:showAsAction="always|withText"
android:title="Search"/>
</menu>
我试图在
中充气menu.xml
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater=new MenuInflater(getBaseContext());
inflater.inflate(R.menu.menu, menu);
return true;
}
但是logcat会抛出错误
10-22 20:03:24.170: W/ResourceType(25679): No package identifier when getting value for resource number 0x00000000
10-22 20:03:24.170: W/MenuInflater(25679): Cannot instantiate class: com.actionbarsherlock.widget.SearchView
10-22 20:03:24.170: W/MenuInflater(25679): java.lang.reflect.InvocationTargetException
10-22 20:03:24.170: W/MenuInflater(25679): at java.lang.reflect.Constructor.constructNative(Native Method)
10-22 20:03:24.170: W/MenuInflater(25679): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.view.MenuInflater$MenuState.newInstance(MenuInflater.java:488)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.view.MenuInflater$MenuState.setItem(MenuInflater.java:447)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.view.MenuInflater$MenuState.addItem(MenuInflater.java:468)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:190)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:112)
10-22 20:03:24.170: W/MenuInflater(25679): at com.example.sk.MainActivity.onCreateOptionsMenu(MainActivity.java:211)
10-22 20:03:24.170: W/MenuInflater(25679): at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:45)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:60)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:165)
10-22 20:03:24.170: W/MenuInflater(25679): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:453)
10-22 20:03:24.170: W/MenuInflater(25679): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:833)
10-22 20:03:24.170: W/MenuInflater(25679): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:3290)
10-22 20:03:24.170: W/MenuInflater(25679): at android.os.Handler.handleCallback(Handler.java:615)
10-22 20:03:24.170: W/MenuInflater(25679): at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 20:03:24.170: W/MenuInflater(25679): at android.os.Looper.loop(Looper.java:137)
10-22 20:03:24.170: W/MenuInflater(25679): at android.app.ActivityThread.main(ActivityThread.java:4921)
10-22 20:03:24.170: W/MenuInflater(25679): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 20:03:24.170: W/MenuInflater(25679): at java.lang.reflect.Method.invoke(Method.java:511)
10-22 20:03:24.170: W/MenuInflater(25679): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
10-22 20:03:24.170: W/MenuInflater(25679): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
10-22 20:03:24.170: W/MenuInflater(25679): at dalvik.system.NativeStart.main(Native Method)
10-22 20:03:24.170: W/MenuInflater(25679): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
10-22 20:03:24.170: W/MenuInflater(25679): at android.content.res.Resources.getValue(Resources.java:1339)
10-22 20:03:24.170: W/MenuInflater(25679): at android.content.res.Resources.getDrawable(Resources.java:976)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.widget.SearchView.getDecoratedHint(SearchView.java:1076)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.widget.SearchView.updateQueryHint(SearchView.java:1096)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.widget.SearchView.setIconifiedByDefault(SearchView.java:595)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.widget.SearchView.<init>(SearchView.java:300)
10-22 20:03:24.170: W/MenuInflater(25679): at com.actionbarsherlock.widget.SearchView.<init>(SearchView.java:252)
10-22 20:03:24.170: W/MenuInflater(25679): ... 24 more
我还将主题保持为android:theme="@style/Theme.Sherlock.Light"
如何解决此错误?
答案 0 :(得分:0)
添加已注释的代码部分并不能完全解决问题,但这很好。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater=new MenuInflater(getBaseContext());
inflater.inflate(R.menu.menu, menu);
// adding this section of code resolved the problem
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
com.actionbarsherlock.widget.SearchView searchView = new com.actionbarsherlock.widget.SearchView(getSupportActionBar().getThemedContext());
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
menu.findItem(R.id.menu_search).setActionView(searchView);
return true;
}
但它在我发布的logcat中显示了相同的警告,我发现这是ActionBarSherlock中众所周知的问题,以获取更多细节click here