将edittext用作searchview时出错

时间:2014-05-05 15:14:30

标签: android android-listview logcat

我有这个列表视图:

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

@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());
            }


            mySearchView = (EditText) findViewById(R.id.searchText);
            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> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_items2, values);
            adapter.sort(new Comparator<String>() {

                @Override
                public int compare(String lhs, String rhs) {
                    return lhs.compareTo(rhs);    
                }
            });

            mySearchView.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    CallActivity.this.adapter.getFilter().filter(cs);   
                }

                @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                          
                }
            });

            mFriendsList.setAdapter(adapter);
        }
    };

我已经尝试设置一个链接到edittext的textwatcher,为了使用edittext过滤listview,它似乎工作良好没有错误的代码,但当我打开应用程序并键入内部的第一个字符edittext崩溃了,我得到了这个LogCat错误:

05-05 17:09:45.794: E/AndroidRuntime(6732): FATAL EXCEPTION: main
05-05 17:09:45.794: E/AndroidRuntime(6732): java.lang.NullPointerException
05-05 17:09:45.794: E/AndroidRuntime(6732):     at com.example.test.CallActivity$1$4.onTextChanged(CallActivity.java:133)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.widget.TextView.sendOnTextChanged(TextView.java:7407)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.widget.TextView.handleTextChanged(TextView.java:7469)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9218)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:676)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:435)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:333)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.os.Looper.loop(Looper.java:137)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at android.app.ActivityThread.main(ActivityThread.java:4867)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at java.lang.reflect.Method.invokeNative(Native Method)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at java.lang.reflect.Method.invoke(Method.java:511)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
05-05 17:09:45.794: E/AndroidRuntime(6732):     at dalvik.system.NativeStart.main(Native Method)

有关为什么会发生这种情况或如何修改代码以使其正常工作的任何提示?

2 个答案:

答案 0 :(得分:1)

您确定在getFilter方法中始终返回非空过滤器吗?

将此行分隔为组件以查看NullPointerException发生的确切位置。变化:

CallActivity.this.adapter.getFilter().filter(cs);

ArrayAdapter<String> myAdapter = CallActivity.this.adapter;
Filter filter = myAdapter.getFilter();
filter.filter(cs);

答案 1 :(得分:1)

onCreate()中,初始化成员变量adapter而不是局部变量。改变这个

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_items2, values);

adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_items2, values);