我正在构建一个应用程序,每当用户键入任何内容时,我都需要在其中使用 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
无法弄清楚我在做什么错。请指出我。