Ich CursorAdapter
und ListView
mit meiner Liste von Songs und Filter in searchView
angezeigt werden, aber das Problem ist, wenn ich das searchView
Symbol tippen habe ich diesen Fehler habe und zeige die Fehler an diesem CodeClientCursorAdapter.getFilter()‘auf einer Null-Objekt Referenz
adapter.getFilter().filter(newText);
Hier ist mein Code
Unter 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;
}
});
die Cursor
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")));
}
}
dann haben Aufruf bekam ich Dieser Fehler
Prozess: com.example.mcgyver.sdalyrics, PID: 5851
java.lang.NullPointerException: Versuch virtuelle Methode ‚android.widget.Filter com.example.mcgyver aufzurufen .sdalyrics.ClientCursorAdapter.getFilter()‘auf eine null Objektreferenz bei com.example.mcgyver.sdalyrics.MainActivity $ 2.onQueryTextChange (MainActivity.java:125) bei android.support.v7.widget.SearchView. onTextChanged (SearchView.java:1232) bei android.support.v7.widget.SearchView $ 12.onTextChanged (SearchView.java:1762)
Dies liegt daran, Sie ClientCursorAdapter.getFilter() vor Initialise anrufen. zuerst ClientCursorAdapter initialisieren und dann – Anil
anrufen Ja, ich habe es gefunden. Ich habe es 2 Mal initialisiert, deshalb habe ich null Wert. danke für Ihre Hilfe. @Anil –