Android过滤列表视图尝试调用接口方法' int java.util.List.size()'在null对象引用上

时间:2015-07-02 00:15:21

标签: java android listview nullpointerexception searchview

我的目标是用研究过滤列表视图,因为" 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);
    }
}

3 个答案:

答案 0 :(得分:0)

也许您没有为Lis​​tView设置适配器?

BTW - 为什么要命名一个与其类类型同名的变量? (ListView ListView)

答案 1 :(得分:0)

尝试在您的adatper中更改此内容:

public int getCount() {
if(filteredListData==null){
Log.v("LOG","Warning, null filteredListData");
return 0;
}else{
return filteredListData.size();
}
}

答案 2 :(得分:-1)

查看错误:

java.lang.NullPointerException...
            at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)

特别地:

at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)

您的错误位于SimpleAdapter.java的第93行。你必须看那里。