0

これを削除しない限り、例外が発生します。

android:targetSdkVersion = "15"

私はここSOの別のスレッドでそれを見つけました。

しかし、私は数日間、そのtargetSdkVersionでこれを実行しました。これが私のコードです:

public class MainActivity extends BaseActivity {
    private TextView textView;
    private String url = "http://www.backcountryskiers.com/sac/sac-full.html";
    private ImageView image;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.todaysReport);
        image = (ImageView) findViewById(R.id.dangerRose);

        fetcher task = new fetcher();
        task.execute();
    }

    public static Bitmap getBitmapFromURL(String src) {
        try {
            Log.e("src", src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            Log.e("Bitmap", "returned");
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Exception", e.getMessage());
            return null;
        }

    }

    class fetcher extends AsyncTask<String, Void, String> {
        private ProgressDialog dialog = new ProgressDialog(MainActivity.this);
        private Document doc = null;
        private Elements content = null;
        private Document parse = null;
        private String results = null;
        private Element dangerRatingImg = null;
        private String dangerRatingSrc = null;
        private Bitmap bimage;

        @Override
        protected String doInBackground(String... params) {

            try {
                // bimage = getBitmapFromURL(drUrl);
                doc = Jsoup.connect(url).get();
                Log.e("Jsoup", "...is working...");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e("Exception", e.getMessage());
            }

            content = doc.select("#content");
            parse = Jsoup.parse(doc.html());
            results = doc.select("#content").outerHtml();

            return results;
        }

        @Override
        protected void onPostExecute(String result) {
            // smooth out the long scrolling...
            textView.setMovementMethod(ScrollingMovementMethod.getInstance());

            // find rating image...
            dangerRatingImg = doc.select("img").first();
            dangerRatingSrc = dangerRatingImg.absUrl("src");
            // Get the rating image
            bimage = getBitmapFromURL(dangerRatingSrc);
            image.setImageBitmap(bimage);
            image.setPadding(10, 10, 10, 10);
            image.setScaleType(ScaleType.FIT_XY);

            // return the summary
            results = parse.select("#reportSummary").outerHtml();
            textView.setText(Html.fromHtml(results));
            textView.setPadding(10, 10, 10, 10);
            // ditch the dialog, it's all loaded.
            dialog.dismiss();

        }

        @Override
        protected void onPreExecute() {
            // before we get the async results show this
            dialog.setMessage("Loading Latest Update from the Sierra Avalanche Center...");
            dialog.show();
        }

    }

}

私はネットワークに接続していて、そのtargetSdkVersionを取り除いても結果をうまく見ることができます...しかし、それは正しくないことを私は知っています。みんな、ありがとう。

編集:

11-09 08:24:55.316: D/dalvikvm(9165): GC_CONCURRENT freed 148K, 3% free 11365K/11655K, paused 12ms+12ms, total 39ms
11-09 08:24:55.410: D/dalvikvm(9165): GC_CONCURRENT freed 331K, 5% free 11484K/11975K, paused 1ms+1ms, total 20ms
11-09 08:24:55.418: E/Jsoup(9165): ...is working...
11-09 08:24:55.558: D/dalvikvm(9165): GC_CONCURRENT freed 465K, 5% free 11506K/12103K, paused 12ms+12ms, total 39ms
11-09 08:24:55.558: E/src(9165): http://www.sierraavalanchecenter.org/sites/default/files/images/danger_icons_and_bars/0_nodangerrate.png
11-09 08:24:55.558: D/AndroidRuntime(9165): Shutting down VM
11-09 08:24:55.558: W/dalvikvm(9165): threadid=1: thread exiting with uncaught exception (group=0x40bc2300)
11-09 08:24:55.566: E/AndroidRuntime(9165): FATAL EXCEPTION: main
11-09 08:24:55.566: E/AndroidRuntime(9165): android.os.NetworkOnMainThreadException
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at com.backcountryskiers.avalanche.report.norcal.MainActivity.getBitmapFromURL(MainActivity.java:49)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at com.backcountryskiers.avalanche.report.norcal.MainActivity$fetcher.onPostExecute(MainActivity.java:102)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at com.backcountryskiers.avalanche.report.norcal.MainActivity$fetcher.onPostExecute(MainActivity.java:1)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.AsyncTask.finish(AsyncTask.java:631)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.os.Looper.loop(Looper.java:137)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
11-09 08:24:55.566: E/AndroidRuntime(9165):     at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

7

あなたはgetBitmapFromURLから呼んでいますonPostExecute

bimage = getBitmapFromURL(dangerRatingSrc);

onPreExecuteonPostExecuteUIスレッドで実行します。このコードはdoInBackground(新しいスレッドで実行される部分であるため)範囲内にある必要があります。


更新:(コメントで説明されているように)次のようにコードを再編成できます:

@Override
protected String doInBackground(String... params) {
    try {
        doc = Jsoup.connect(url).get();
        Log.e("Jsoup", "...is working...");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("Exception", e.getMessage());
    }

    content = doc.select("#content");
    parse = Jsoup.parse(doc.html());
    results = doc.select("#content").outerHtml();

    // find rating image...
    dangerRatingImg = doc.select("img").first();
    dangerRatingSrc = dangerRatingImg.absUrl("src");
    // Get the rating image
    bimage = getBitmapFromURL(dangerRatingSrc);

    return results;
}

@Override
protected void onPostExecute(String result) {
    // smooth out the long scrolling...
    textView.setMovementMethod(ScrollingMovementMethod.getInstance());
    // Set the rating image
    image.setImageBitmap(bimage);
    image.setPadding(10, 10, 10, 10);
    image.setScaleType(ScaleType.FIT_XY);

    // return the summary
    results = parse.select("#reportSummary").outerHtml();
    textView.setText(Html.fromHtml(results));
    textView.setPadding(10, 10, 10, 10);
    // ditch the dialog, it's all loaded.
    dialog.dismiss();
}
于 2012-11-09T16:26:49.480 に答える