NullPointerException异常动态AutoCompleteTextView [英] NullPointerException on dynamic AutoCompleteTextView

查看:210
本文介绍了NullPointerException异常动态AutoCompleteTextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些用户报告这个错误:

Some users are reporting this error:

java.lang.NullPointerException
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
at android.widget.AutoCompleteTextView$PopupDataSetObserver$1.run(AutoCompleteTextView.java:1670)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

下面是我的code片断:

Here is my code snippet:

    private List<City> autoCompleteCities = new ArrayList<City>();
    private List<City> autoCompleteCitiesOld = new ArrayList<City>();
    private ArrayAdapter<String> autoCompleteAdapter;
    private AutoCompleteTextView cityView;

    ...

    autoCompleteAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
    autoCompleteAdapter.setNotifyOnChange(true);

    cityView = (AutoCompleteTextView) findViewById(R.id.city);      

    cityView.setAdapter(autoCompleteAdapter);
    cityView.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            LogUtil.i("!!!!!!!!!!!!! ontextchanged", s.toString());
            autoCompleteAdapter.clear();
            autoCompleteCitiesOld = autoCompleteCities;
            if (s.toString().length() > 2) {
                autoCompleteCities = search(s.toString());
                for (City city : autoCompleteCities) {
                    autoCompleteAdapter.add(city.getDisplayName());
                }
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

它不发生的我,但我有足够的错误报道才知道它发生了几个用户。任何想法有什么不好?为什么会出现这种情况只有少数用户?

It doesnt happen for me, but I have enough error reports to know it happens for several users. Any idea what is wrong? Why does this happen to only a handful of users?

我发现这个<一个href=\"http://stackoverflow.com/questions/5777357/arrayadapter-getcount-nullpointerexception\">post但把新的自动完成值取在AsyncTask的意思,结果总是落后用户已经进入了什么样的一个字符。

I found this post but putting the fetching of new autocomplete values in an AsyncTask meant that the results always were one character behind what the user had entered.

推荐答案

你在哪里传递的ArrayList到一个ArrayAdapter。该NullPointerException异常而在计数的ArrayAdapter列表lenght抛出。但是,列表不会传递到适配器,所以在默认适配器列表对象包含null。

Where are you passing the ArrayList to the ArrayAdapter. The NullPointerException throws while counting the list lenght in ArrayAdapter. But the list is not passed to Adapter, so the list object in default Adapter contains null.

这篇关于NullPointerException异常动态AutoCompleteTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆