1

我正在构建一个应用程序,每当用户键入任何内容时,我都需要在其中使用 autocompletetextview 带来自动建议。

这是我的代码:

public List<String> suggest;

HttpURLConnection con = null;
            String newText = key[0];
            newText = newText.trim();
            newText = newText.replace(" ", "+");
            try {
                // Check if task has been interrupted
                if (Thread.interrupted())
                    throw new InterruptedException();
                // Build RESTful query for Google API
                // String q = URLEncoder.encode(original, "UTF-8");
                URL url = new URL(
                        "http://google.com/complete/search?output=toolbar&q="
                                + newText);
                con = (HttpURLConnection) url.openConnection();
                con.setReadTimeout(10000 /* milliseconds */);
                con.setConnectTimeout(15000 /* milliseconds */);
                con.setRequestMethod("GET");
                // con.addRequestProperty("Referer",
                // "http://www.pragprog.com/titles/eband3/hello-android");
                con.setDoInput(true);
                // Start the query
                con.connect();

                // Check if task has been interrupted
                if (Thread.interrupted())
                    throw new InterruptedException();
                // Read results from the query
                XmlPullParser parser = Xml.newPullParser();
                parser.setInput(con.getInputStream(), null);
                int eventType = parser.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    String name = parser.getName();
                    if (eventType == XmlPullParser.START_TAG
                            && name.equalsIgnoreCase("suggestion")) {
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            if (parser.getAttributeName(i)
                                    .equalsIgnoreCase("data")) {
                                suggest.add(parser.getAttributeValue(i));
                            }
                        }
                    }
                    eventType = parser.next();
                }

                // Check if task has been interrupted
                if (Thread.interrupted())
                    throw new InterruptedException();

            } catch (Exception e) {

            }

这给了我日志错误:

10-22 11:36:46.531: E/AndroidRuntime(22291): FATAL EXCEPTION: main
10-22 11:36:46.531: E/AndroidRuntime(22291): java.lang.NullPointerException
10-22 11:36:46.531: E/AndroidRuntime(22291):    at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
10-22 11:36:46.531: E/AndroidRuntime(22291):    at android.widget.AutoCompleteTextView$PopupDataSetObserver$1.run(AutoCompleteTextView.java:1670)
10-22 11:36:46.531: E/AndroidRuntime(22291):    at android.os.Handler.handleCallback(Handler.java:587)
10-22 11:36:46.531: E/AndroidRuntime(22291):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 11:36:46.531: E/AndroidRuntime(22291):    at android.os.Looper.loop(Looper.java:130)
10-22 11:36:46.531: E/AndroidRuntime(22291):    at  android.app.ActivityThread.main(ActivityThread.java:3689)
10-22 11:36:46.531: E/AndroidRuntime(22291):    at java.lang.reflect.Method.invokeNative(Native Method)
10-22 11:36:46.531: E/AndroidRuntime(22291):    at java.lang.reflect.Method.invoke(Method.java:507)
10-22 11:36:46.531: E/AndroidRuntime(22291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-22 11:36:46.531: E/AndroidRuntime(22291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-22 11:36:46.531: E/AndroidRuntime(22291):    at dalvik.system.NativeStart.main(Native Method)
10-22 11:36:46.539: E/(1459): Dumpstate > /data/log/dumpstate_app_error

无法弄清楚我在做什么错。请指出我。

4

2 回答 2

1

首先,让我们初始化对您的列表的引用。现在确实为空。

代替

public List<String> suggest;

写这样的东西:

public List<String> suggest = new LinkedList<String>();

这应该可以解决您的 NPE,并且可以提出一些新问题 ;)。

于 2012-10-22T10:43:24.640 回答
-1

谢谢,但我解决了我的问题。我没有使用 XML,而是使用 JSON 解析,这比 XML 解析快得多。我使用了 wikisuggest api 并对其进行了解析,以将建议显示为用户类型。很抱歉,但我认为现在没有赏金。

By the way thank you all for giving it attention and trying to solve my problem, I really appreciate all.

于 2012-10-25T04:48:12.453 回答