0

ボタンをクリックするだけで、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
4

1 に答える 1

0

行番号 79 がこのコードの場合:

is.close();

次に、変数isは null です。これは、コードが次のことを意味します。

is = new InputStreamReader(response.getEntity().getContent());

実行されなかったため、エラーは次の行に表示されます。

HttpResponse response = httpClient.execute(httpGet, localContext);

その行はエラーをスローするため、内部のコードが実行さfinallyれます。isnullis.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タグを確認します。

于 2013-06-25T17:42:11.253 に答える