我的目标是用一个研究来过滤列表视图,当你正在寻找一个有他的名字的朋友时,它被称为"Whatsapp"。当我试图在搜索框内写入时,我遇到了一个错误,只有按下搜索图标时才会出现这个错误。
这是我的错误:
Process: org.testing.an_app, PID: 15386
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
at android.widget.AdapterView.checkFocus(AdapterView.java:717)
at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:840)
at android.widget.AbsListView$AdapterDataSetObserver.onInvalidated(AbsListView.java:6137)
at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:50)
at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:59)
at android.widget.SimpleAdapter$SimpleFilter.publishResults(SimpleAdapter.java:383)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
这是我用自定义项目创建列表视图的代码:
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class Friends_List_Screen extends Activity {
private SearchView mSearchView;
private ListView ListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends_list);
ListView = (ListView) findViewById(R.id.Friends);
Create_List();
ListView.setTextFilterEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.friend_list_screen_menu, menu);
mSearchView = (SearchView) menu.findItem(R.id.search).getActionView();
if (Integer.valueOf(android.os.Build.VERSION.SDK) > 7) {
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
ListView = (ListView) findViewById(R.id.Friends);
SimpleAdapter adapter = (SimpleAdapter) ListView.getAdapter();
if (TextUtils.isEmpty(s)) {
adapter.getFilter().filter("");
} else {
adapter.getFilter().filter(s.toString().toLowerCase());
}
return true;
}
});
}
final MenuItem searchMI = (MenuItem) menu.findItem(R.id.search);
mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean queryTextFocused) {
if (!queryTextFocused) {
searchMI.collapseActionView();
}
}
});
return true;
}
private void Create_List(){
ArrayList<Friend_OBJ> Friend_OBJList=new ArrayList<Friend_OBJ>();
Friend_OBJ [] people={
new Friend_OBJ("X","B",R.drawable.profile_icon),
new Friend_OBJ("Y", "Q", R.drawable.profile_icon),
new Friend_OBJ("Z","D",R.drawable.profile_icon),
new Friend_OBJ("W","X",R.drawable.profile_icon)};
Random r=new Random();
for(int i=0;i<100;i++){
Friend_OBJList.add(people[r.nextInt(people.length)]);
}
ArrayList<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>();
for(int i=0;i<Friend_OBJList.size();i++){
Friend_OBJ p=Friend_OBJList.get(i);
HashMap<String,Object> Friend_OBJMap=new HashMap<String, Object>();
Friend_OBJMap.put("image", p.getPhotoRes());
Friend_OBJMap.put("name", p.getName());
Friend_OBJMap.put("surname", p.getSurname());
data.add(Friend_OBJMap);
}
String[] from={"image","name","surname"};
int[] to={R.id.personImage,R.id.personName,R.id.personSurname};
SimpleAdapter adapter=new SimpleAdapter(
getApplicationContext(),
data,
R.layout.friends_list_item,
from,
to);
ListView.setAdapter(adapter);
}
}
也许您没有为ListView设置适配器?
BTW-为什么要用与其类类型相同的名称命名变量?(ListView ListView)
尝试在您的adatpar:中更改此项
public int getCount() {
if(filteredListData==null){
Log.v("LOG","Warning, null filteredListData");
return 0;
}else{
return filteredListData.size();
}
}
查看错误:
java.lang.NullPointerException...
at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
特别是:
at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
您的错误在SimpleAdapter.java的第93行。您必须查看那里。