我试图在点击按钮的Android应用程序中使用Yahoo Finance API获取货币报价。 我的代码:
String urlStr = "http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=l1&e=.csv";
httpGet = new HttpGet(urlStr);
InputStreamReader is = null;
String value = "";
try {
HttpResponse response = httpClient.execute(httpGet, localContext);
is = new InputStreamReader(response.getEntity().getContent());
BufferedReader reader = new BufferedReader(is);
String line = reader.readLine();
String[] RowData = line.split(",");
value = RowData[0];
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
} finally {
try {
is.close();
} catch (IOException e) {
// handle exception
}
}
((TextView) findViewById(R.id.value)).setText(value);
应用程序崩溃,提供java.lang.IllegalStateException。我在这里缺少什么?
堆栈跟踪:
06-25 16:40:19.154: E/AndroidRuntime(2426): FATAL EXCEPTION: main
06-25 16:40:19.154: E/AndroidRuntime(2426): java.lang.IllegalStateException: Could not execute method of the activity
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.view.View$1.onClick(View.java:3599)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.view.View.performClick(View.java:4204)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.view.View$PerformClick.run(View.java:17355)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.os.Handler.handleCallback(Handler.java:725)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.os.Looper.loop(Looper.java:137)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-25 16:40:19.154: E/AndroidRuntime(2426): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 16:40:19.154: E/AndroidRuntime(2426): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 16:40:19.154: E/AndroidRuntime(2426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-25 16:40:19.154: E/AndroidRuntime(2426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-25 16:40:19.154: E/AndroidRuntime(2426): at dalvik.system.NativeStart.main(Native Method)
06-25 16:40:19.154: E/AndroidRuntime(2426): Caused by: java.lang.reflect.InvocationTargetException
06-25 16:40:19.154: E/AndroidRuntime(2426): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 16:40:19.154: E/AndroidRuntime(2426): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.view.View$1.onClick(View.java:3594)
06-25 16:40:19.154: E/AndroidRuntime(2426): ... 11 more
06-25 16:40:19.154: E/AndroidRuntime(2426): Caused by: java.lang.NullPointerException
06-25 16:40:19.154: E/AndroidRuntime(2426): at com.fuzzy.currencyconverter.CurrencyConverterActivity.fetch(CurrencyConverterActivity.java:79)
06-25 16:40:19.154: E/AndroidRuntime(2426): ... 14 more
答案 0 :(得分:0)
如果第79行是此代码:
is.close();
然后变量is
为空,表示代码:
is = new InputStreamReader(response.getEntity().getContent());
未执行,因此错误出现在这一行:
HttpResponse response = httpClient.execute(httpGet, localContext);
该行会抛出错误,因此finally
内的代码会被执行,因为is
为null
,然后调用is.close()
会抛出NullPointerException
。
在访问之前,您必须检查is
是否为null
,因为有可能未初始化。此外,您必须检查HttpResponse response = httpClient.execute(httpGet, localContext);
返回的错误,以便了解会发生什么。
改变这个:
} catch (IOException e) {
Log.d("YourApp", e.getMessage());
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
// handle exception
}
}
并检查您的LogCat是否有YourApp
标记。