android - ClientCursorAdapter.getFilter()' on a null object reference -
i'm using cursoradapter
, listview
display list of songs , filter in searchview
problem when tap searchview
icon i've got error , pointing error code
adapter.getfilter().filter(newtext);
here code
under 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; } });
calling 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"))); } }
then i've got error
process: com.example.mcgyver.sdalyrics, pid: 5851
java.lang.nullpointerexception: attempt invoke virtual method 'android.widget.filter com.example.mcgyver.sdalyrics.clientcursoradapter.getfilter()' on null object reference @ 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)
the clientcursoradapter has not yet created when oncreateoptionsmenu. use following code,
if(adapter!=null){ adapter.getfilter().filter(newtext); }
Comments
Post a Comment