0

我有一个菜单,它维护一个CustomSearchView. 因为我想在 3 个选项卡(片段)之间切换,MainActivity所以我制作了 3 个与每个选项卡相关的菜单 .xml 文件。对于女巫操作栏的菜单,我使用onPrepareOptionMenu以下内容:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (menu != null && menu.hasVisibleItems())
        menu.clear();

    switch ((int) viewPager.getCurrentItem()) {
    case 0:
        Log.d(TAG, "onPrepareOptionsMenu " + viewPager.getChildCount()
                + "            " + viewPager.getCurrentItem());
        getMenuInflater().inflate(R.menu.searching_menu, menu); //line 99
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            MenuItem item = menu.findItem(R.id.searchview);
            customSearchView = (CustomSearchView) item.getActionView(); //line 103
            customSearchView.setSearchableInfo(searchManager
                    .getSearchableInfo(getComponentName()));
            customSearchView.setSaveSearchTextState(true);
            customSearchView.setIconifiedByDefault(false);
            customSearchView
                    .setOnStatefulQueryTextListener(new android.widget.SearchView.OnQueryTextListener() {

                        @Override
                        public boolean onQueryTextChange(String query) {
                            searchingFragment.loadResultFound(query);
                            return true;
                        }

                        @Override
                        public boolean onQueryTextSubmit(String query) {
                            return true;
                        }
                    });
        }
        mSearchBySpinner = (Spinner) menu.findItem(R.id.search_by_spinner);

        View view = ((MenuItem) mSearchBySpinner).getActionView();
        if (view instanceof Spinner) {
            Spinner spinner = (Spinner) view;
            spinner.setAdapter(ArrayAdapter.createFromResource(
                    getApplicationContext(), R.array.searchby,
                    android.R.layout.simple_spinner_dropdown_item));
        }

        break;
    case 1:
        Log.d(TAG, "onPrepareOptionsMenu " + viewPager.getChildCount()
                + "            " + viewPager.getCurrentItem());
        getMenuInflater().inflate(R.menu.sort_menu, menu);
        break;
    case 2:
        Log.d(TAG, "onPrepareOptionsMenu " + viewPager.getChildCount()
                + "            " + viewPager.getCurrentItem());
        getMenuInflater().inflate(R.menu.favorite_menu, menu);
        break;
    }

    setMenu(menu);
    return super.onPrepareOptionsMenu(menu);
}

这是 search_menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/searchview"
    android:actionViewClass="com.haanz.K4Droid.ui.CustomSearchView"
    android:icon="@android:drawable/ic_search_category_default"
    android:orderInCategory="99"
    android:showAsAction="collapseActionView|ifRoom"
    android:title="@string/searching"/>

<item
    android:id="@+id/search_by_spinner"
    android:actionViewClass="android.widget.Spinner"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="spinner"/>
<item
    android:id="@+id/info"
    android:icon="@android:drawable/ic_dialog_info"
    android:orderInCategory="98"
    android:showAsAction="collapseActionView"
    android:title="@string/searching"/>
</menu>

当我在设备上运行项目时,它会抛出一个像这样的 Logcat:

    08-30 11:11:05.483: D/MainActivity(13881): onPrepareOptionsMenu 2            0
    08-30 11:11:05.488: W/MenuInflater(13881): Cannot instantiate class: com.haanz.K4Droid.ui.CustomSearchView
    08-30 11:11:05.488: W/MenuInflater(13881): java.lang.ClassNotFoundException: com.haanz.K4Droid.ui.CustomSearchView
    08-30 11:11:05.488: W/MenuInflater(13881):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at java.lang.ClassLoader.loadClass(ClassLoader.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at java.lang.ClassLoader.loadClass(ClassLoader.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at android.view.MenuInflater$MenuState.newInstance(MenuInflater.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at android.view.MenuInflater$MenuState.setItem(MenuInflater.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at android.view.MenuInflater$MenuState.addItem(MenuInflater.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at android.view.MenuInflater.parseMenu(MenuInflater.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at android.view.MenuInflater.inflate(MenuInflater.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at com.haanz.K4Droid.ui.MainActivity.onPrepareOptionsMenu(MainActivity.java:99)
    08-30 11:11:05.488: W/MenuInflater(13881):  at android.app.Activity.onPreparePanel(Activity.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:469)
    08-30 11:11:05.488: W/MenuInflater(13881):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at android.os.Handler.handleCallback(Handler.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at android.os.Handler.dispatchMessage(Handler.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at android.os.Looper.loop(Looper.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at android.app.ActivityThread.main(ActivityThread.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-30 11:11:05.488: W/MenuInflater(13881):  at java.lang.reflect.Method.invoke(Method.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
    08-30 11:11:05.488: W/MenuInflater(13881):  at dalvik.system.NativeStart.main(Native Method)
    08-30 11:11:05.498: W/dalvikvm(13881): threadid=1: thread exiting with uncaught exception (group=0x419f24d0)
    08-30 11:11:05.498: E/AndroidRuntime(13881): FATAL EXCEPTION: main
    08-30 11:11:05.498: E/AndroidRuntime(13881): java.lang.NullPointerException
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at com.haanz.K4Droid.ui.MainActivity.onPrepareOptionsMenu(MainActivity.java:104)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at android.app.Activity.onPreparePanel(Activity.java)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:469)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at android.os.Handler.handleCallback(Handler.java)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at android.os.Handler.dispatchMessage(Handler.java)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at android.os.Looper.loop(Looper.java)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at android.app.ActivityThread.main(ActivityThread.java)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at java.lang.reflect.Method.invokeNative(Native Method)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at java.lang.reflect.Method.invoke(Method.java)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
    08-30 11:11:05.498: E/AndroidRuntime(13881):    at dalvik.system.NativeStart.main(Native Method)
    08-30 11:11:05.518: D/Process(13881): killProcess, pid=13881
    08-30 11:11:05.518: D/Process(13881): dalvik.system.VMStack.getThreadStackTrace(Native Method)
    08-30 11:11:05.518: D/Process(13881): java.lang.Thread.getStackTrace(Thread.java)
    08-30 11:11:05.518: D/Process(13881): android.os.Process.killProcess(Process.java)
    08-30 11:11:05.518: D/Process(13881): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java)
    08-30 11:11:05.518: D/Process(13881): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java)
    08-30 11:11:05.518: D/Process(13881): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java)

谁能告诉我为什么会发生这种情况?当它大喊“无法实例化类:com.Haanz.K4Droid.ui.CustomSearchView ”时,我找不到任何东西。任何帮助将不胜感激。

4

0 回答 0