Unable to use search view to filter list view

I want to filter my listview using the input in search view. I have following code:

package com.example.shiza.dailyquranverses;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.shiza.dailyquranverses.DailyQuranMethods;

public class SearchResultsActivity extends ActionBarActivity implements SearchView.OnQueryTextListener{

    Toolbar toolbar;
    ListView listView;
    SearchView searchView;
    ArrayAdapter<String> adapter;
    DailyQuranMethods dailyQuranMethods = new DailyQuranMethods();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);

        toolbar = (Toolbar) findViewById(R.id.app_bar_search_results);
        toolbar.setLogo(R.mipmap.ic_launcher);
        toolbar.setTitle(" Daily Qura'n Verses");
        setSupportActionBar(toolbar);
        Toast.makeText(this, "Hello from onCreate method", Toast.LENGTH_LONG).show();

//       using searchview to display data in the app

        searchView = (SearchView) findViewById(R.id.mySearchView);


        listView = (ListView) findViewById(R.id.listView);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dailyQuranMethods.getQuranVerses(this));

        listView.setAdapter(adapter);
        searchView.setOnQueryTextListener(this);

    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }
}

Now onQueryTextChange, the application get stopped. Looks like the following method has the issue:

public boolean onQueryTextChange(String newText) { adapter.getFilter().filter(newText); return false; }

Please help me to solve this.

Edit 1: Logcat is :

06-19 14:40:25.815 3259-3259/com.example.shiza.dailyquranverses E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330) at android.widget.AdapterView.checkFocus(AdapterView.java:700) at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:823) at android.widget.AbsListView$AdapterDataSetObserver.onInvalidated(AbsListView.java:5600) at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:47) at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:59) at android.widget.ArrayAdapter$ArrayFilter.publishResults(ArrayAdapter.java:513) at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invo ke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method)

链接地址: http://www.djcxy.com/p/92964.html

上一篇: 如何在多个参数上使用SearchView过滤RecyclerView

下一篇: 无法使用搜索视图来过滤列表视图