PackageCom.example.euphemiaxiao.classsearch;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;//import Android.support.v7.widget.SearchView;ImportAndroid.widget.SearchView;Importandroid.text.TextUtils;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView; Public classClasssearchextendsAppcompatactivityImplementssearchview.onquerytextlistener{PrivateSearchview SV; PrivateListView LV; PrivateArrayadapter<string>adapter; Private FinalString[] mstrings={"2014211501", "2014211502", "2014211503", "2014211504", "2015211501", "2015211502", "2015211503", "2015211504"}; //private arraylist<string> alist;@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.class_search); LV=(ListView) Findviewbyid (r.id.lv); Adapter=NewArrayadapter<string> ( This, Android. R.layout.simple_list_item_1,mstrings); Lv.setadapter (adapter); //Lv.setadapter (New arrayadapter<string> (this,android. r.layout.simple_list_item_1,mstrings));Lv.settextfilterenabled (true);//set LV can be filteredsv=(Searchview) Findviewbyid (R.ID.SV); Sv.seticonifiedbydefault (false);//Setting the Searchview default does not automatically shrink to an iconSv.setonquerytextlistener ( This);//setting up event listenersSv.setsubmitbuttonenabled (true);//Set Display Search buttonSv.setqueryhint ("Input class number");//Set default display text } //Fire This method when you click the Search button@Override Public Booleanonquerytextsubmit (String query) {return false; } //The method is fired when the user enters a character@Override Public BooleanOnquerytextchange (String newtext) {if(Textutils.isempty (NewText)) {//Clearing the ListView filterLv.cleartextfilter (); } Else {//filter list items for a ListView by using user-entered contentLv.setfiltertext (NewText); } return false; }}
Above is the Classsearch code
About the ListView, begin to error in ARRAYADAPTER<STRING>, the error is pointing to a null pointer. The main reason for the error here is Setcontentview (R.layout.class_search), because the direction of the wrong so rightfully called has been a null pointer.Be sure to see which layout you are calling. Before there are two layout, reference errors caused by the error, and later I was impatient, directly deleted another, only to find the error here, written another layout. about Searchview, error insv= (Searchview) Findviewbyid (R.ID.SV), the error is:Android.widget.SearchView cannot is cast to Android.support.v7.widget.SearchViewThe solution to this error iswill beImport Android.support.v7.widget.SearchView change to import Android.widget.SearchViewsolved (though not known for God's horse)
Android in Searchview