java - 搜索Widget和NullPointer

时间:2018-02-27 18:34:18

标签: java android xml nullpointerexception searchview

例外是 NullPointerException 。有关searchview的错误信息。当我运行这个程序不起作用。大多数人关于xml的“android”到“app”的例外,但我的仍然无法正常工作

    Process: com.mcht.mynotes, PID: 24583
                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
                                                   at com.mcht.mynotes.MainActivity.onCreateOptionsMenu(MainActivity.java:61)
                                                   at android.app.Activity.onCreatePanelMenu(Activity.java:3142)
                                                   at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:328)
                                                   at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
                                                   at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
                                                   at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1370)
                                                   at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1650)
                                                   at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:154)
                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

有错误的代码;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);


    //search
    SearchView searchView = (SearchView)this.findViewById(R.id.app_bar_search);
    SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));//line 61
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            return false;
        }
    });


    return true;
}

和xml文件,我有icc_search_black_24dp.png。如果你问为什么我不使用“ic_search_black_24dp”它仅用于测试目的;

<?xml version="1.0" encoding="utf-8"?>

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

<item
    android:id="@+id/newNote"
    android:icon="@drawable/ic_note_add_black_24dp"
    android:title="@string/item"
    app:showAsAction="ifRoom" />
<item
    android:id="@+id/app_bar_search"
    android:checkable="false"
    android:enabled="false"
    android:icon="@drawable/icc_search_black_24dp"
    android:title="Search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always" />

1 个答案:

答案 0 :(得分:0)

你没有按照你应该的方式获得视图。试试这个:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);

    //search
    SearchView searchView = (SearchView)menu.findItem(R.id.app_bar_search);
    ...
}