使用EditText错误过滤ListView

时间:2014-05-02 14:19:26

标签: android android-listview android-edittext

我有想要过滤的listview的代码:

private EditText mySearchView;
private ArrayAdapter<String> friendsListAdapter;
private ListView listView;

@Override
        public void onComplete(List<Profile> friends) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mSpinner.setVisibility(View.GONE);
                    mSpinner.clearAnimation();
                }
                });

            // populate list
            List<String> values = new ArrayList<String>();
            for (Profile profile : friends) {
                //profile.getInstalled();
                values.add(profile.getName());
            }

            listView = (ListView) findViewById(R.id.list);
            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> av, final View view, final int i, long i2) {

                  Animation pushLeftIn = AnimationUtils.loadAnimation(CallActivity.this, R.anim.jump_no_fade);
                  view.startAnimation(pushLeftIn);

            }
            });

            ArrayAdapter<String> friendsListAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_items2, values);
            friendsListAdapter.sort(new Comparator<String>() {
                @Override
                public int compare(String lhs, String rhs) {
                    return lhs.compareTo(rhs);    
                }
            });

            mFriendsList.setAdapter(friendsListAdapter);

            mySearchView = (EditText)findViewById(R.id.searchText);

            mySearchView.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub
                    CallActivity.this.friendsListAdapter.getFilter().filter(arg0);
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                        int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub

                }
            });
        }
    };

我尝试将名为mySearchView的edittext过滤到listview,当输入时,但是当我打开应用程序并在edittext中输入时没有任何反应,代码有什么问题或者我怎么能修改它?

当我加载listview时,我也遇到了这个logcat错误:

05-02 16:23:47.075: E/com.example.test.CallActivity(27640): Bad thing happened
05-02 16:23:47.075: E/com.example.test.CallActivity(27640): java.lang.NullPointerException
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at    com.example.test.CallActivity$1.onComplete(CallActivity.java:127)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at com.example.test.CallActivity$1.onComplete(CallActivity.java:1)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at com.sromku.simple.fb.actions.GetAction$1.onCompleted(GetAction.java:48)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at com.facebook.Request$4.run(Request.java:1670)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at android.os.Handler.handleCallback(Handler.java:615)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at android.os.Looper.loop(Looper.java:137)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at android.app.ActivityThread.main(ActivityThread.java:4867)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at java.lang.reflect.Method.invoke(Method.java:511)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
05-02 16:23:47.075: E/com.example.test.CallActivity(27640):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您永远不会从XML实体连接mySearchView,也不会在使用它之前创建它。我假设您没有发布所有代码 - 您应该在addTextChangedListener声明中获得NPE。