1

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

我该如何解决这个错误?

4

1 回答 1

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 中是/是众所周知的问题,有关更多详细信息,请单击此处

于 2013-10-22T18:06:17.757 回答