对空引用的ClientCursorAdapter.getFilter()'

时间:2017-07-03 02:29:38

标签: android

我正在使用CursorAdapterListView来显示我的歌曲列表,并在searchView中进行过滤,但问题是当我点击我已经获得的searchView图标时该错误并将错误指向此代码

adapter.getFilter().filter(newText);

这是我的代码

在onCreateOptionsMenu()

 SearchView searchView = (SearchView) item.getActionView();
 searchView.setQueryHint("Search Title/Artist");

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

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

    @Override
    public boolean onQueryTextChange(String newText) {

        adapter.getFilter().filter(newText);
        return false;
    }
});

调用CursorAdapter

  Cursor res = db.query("songs",null,null,null,null,null,"title");


        ClientCursorAdapter adapter = new ClientCursorAdapter(MainActivity.this,res, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
////
////
        lv.setAdapter(adapter);

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="80dp"
    android:background="@color/black">


    <TextView
        android:id="@+id/Title"
        android:layout_width="wrap_content"
        android:layout_height="37.5dp"
        android:text="Title"
        android:paddingLeft="10dp"
        android:textSize="22dp"
        android:gravity="center_vertical"
        android:textColor="#FFFFFF"
        android:layout_alignParentLeft="true" />

    <ToggleButton
        android:id="@+id/minus_one"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:checked="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:textOn=""
        android:textColor="@color/colorAccent"
        android:layout_marginRight="5dp"
        android:background="@drawable/minus_one"
        android:textOff=""
        />
    <ToggleButton
        android:id="@+id/favorite"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/minus_one"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:checked="true"
        android:textOn=""
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_marginRight="5dp"
       android:background="@drawable/favorite"
        android:textOff=""
        />


    <TextView
        android:id="@+id/Artist"
        android:layout_width="wrap_content"
        android:layout_height="37.5dp"
        android:text="Artist"
        android:paddingLeft="10dp"
        android:textColor="#707070"
        android:layout_below="@+id/Title"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#4f5051"
        android:layout_below="@+id/Artist"
        >

    </View>



</RelativeLayout>

ClientCursorAdapter.java

class ClientCursorAdapter extends CursorAdapter {
    Context context;
    public ClientCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        this.context = context;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.row_layout,viewGroup,false);
        return v;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView title = view.findViewById(R.id.Title);
        TextView artist = view.findViewById(R.id.Artist);
        ToggleButton music = view.findViewById(R.id.minus_one);
        ToggleButton favorite = view.findViewById(R.id.favorite);

        title.setText(cursor.getString(cursor.getColumnIndex("TITLE")));
        artist.setText(cursor.getString(cursor.getColumnIndex("ARTIST")));

    }
}

然后我遇到了这个错误

  

处理:com.example.mcgyver.sdalyrics,PID:5851

     

java.lang.NullPointerException:尝试调用虚方法   “android.widget.Filter   com.example.mcgyver.sdalyrics.ClientCursorAdapter.getFilter()'on a   null对象引用                                                                                    在   com.example.mcgyver.sdalyrics.MainActivity $ 2.onQueryTextChange(MainActivity.java:125)                                                                                    在   android.support.v7.widget.SearchView.onTextChanged(SearchView.java:1232)                                                                                    在   android.support.v7.widget.SearchView $ 12.onTextChanged(SearchView.java:1762)

1 个答案:

答案 0 :(得分:1)

onCreateOptionsMenu时尚未创建ClientCursorAdapter。 使用以下代码

if(adapter!=null){
   adapter.getFilter().filter(newText);
}