0

我有一个 Async 正在运行以从我创建的页面中获取数据。它的文本很好,但是当我尝试通过另一个类从图像 src 获取图像时,应用程序强制关闭。这是它强制关闭的代码:

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // tell which region this covers
        getSupportActionBar().setSubtitle("...from Sierra Avalanche Center");
        setContentView(R.layout.activity_fullreport);
        textView = (TextView) findViewById(R.id.todaysReport);
        ivDangerRose = (ImageView) findViewById(R.id.dangerRose);
        fetcher task = new fetcher();
        task.execute();
    }

    // GET THE IMAGE and RETURN IT
    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    class fetcher extends AsyncTask<String, Void, String> {
        private ProgressDialog dialog = new ProgressDialog(
                FullReportActivity.this);
        private Document doc = null;
        private Document parse = null;
        private String results = null;
        private String reportDate = null;
        private Bitmap bimage = null;

        @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());
            }

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

            Element dangerRoseImg = doc.getElementById("reportRose")
                    .select("img").first();
            String dangerRoseSrc = dangerRoseImg.absUrl("src");
            Log.i("Report Rose IMG", dangerRoseSrc);
            bimage = getBitmapFromURL(dangerRoseSrc);
            ivDangerRose.setImageBitmap(bimage);

            return results;
        }

        @Override
        protected void onPostExecute(String result) {
            dialog.dismiss();
            // smooth out the long scrolling...
            textView.setMovementMethod(ScrollingMovementMethod.getInstance());
            reportDate = parse.select("#reportDate").outerHtml();
            textView.setText(Html.fromHtml(reportDate + results));
            textView.setPadding(30, 20, 20, 10);
        }

        @Override
        protected void onPreExecute() {
            dialog.setMessage("Loading Full Report from the Sierra Avalanche Center...");
            dialog.show();
        }

    }

}

我已经单独运行此 Async 以在没有强制关闭的情况下获得这样的图像,除了调用该方法之外,我不明白我在做什么不同:

public class MainActivity extends Activity {
    public String durl = "http://www.sierraavalanchecenter.org/dangerrose.png?a=2955";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    

        new DownloadImageTask((ImageView) findViewById(R.id.dangerrose))
        .execute(durl);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }



    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
          ImageView bmImage;

          public DownloadImageTask(ImageView bmImage) {
              this.bmImage = bmImage;
          }

          protected Bitmap doInBackground(String... urls) {
              String urldisplay = urls[0];
              Bitmap drose = null;
              try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                drose = BitmapFactory.decodeStream(in);
              } catch (Exception e) {
                  Log.e("Error", e.getMessage());
                  e.printStackTrace();
              }
              return drose;
          }

          protected void onPostExecute(Bitmap result) {
              bmImage.setImageBitmap(result);
          }
        }
}

这个类获取图像src并创建一个位图并将其放入ImageView中,这里与我的第一个类有什么不同???

沮丧的。

4

3 回答 3

6

您不能UI从后台线程进行修改。

搬进ivDangerRose.setImageBitmap(bimage);onPostExecute

于 2012-12-04T06:09:01.013 回答
1

在方法 doInBackground

remove --> ivDangerRose.setImageBitmap(bimage);

因为您无法在后台进程中修改 UI。

如果你仍然想要你可以试试 runOnUiThread 方法

于 2012-12-04T06:17:35.630 回答
0

在 doInBackground() 我们不应该访问活动的内容。

于 2012-12-04T06:07:04.170 回答