本文介绍了空指针异常时,从URL获取图像摆在ImageView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过一个网址让我的形象,并试图向设置为我的ImageView。但我发现了一个空指针异常。它甚至没有进入我asynchtask。

有什么我做错了还是没有看到?

 公共类DetailsActivity延伸活动{
    ImageView的缩略图=(ImageView的)findViewById(R.id.btnThumbnail);
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.detail);        最终捆绑额外= getIntent()getExtras()。
        字符串IMG = extras.getString(缩略图);
        新DownloadImageTask((ImageView的)findViewById(R.id.btnThumbnail))
                .execute(http://mysite.com/images/
                        + IMG);
        }    类DownloadImageTask扩展的AsyncTask<弦乐,太虚,位图> {        公共DownloadImageTask(ImageView的bmImage){
            缩略图= bmImage;
        }        保护位图doInBackground(字符串的URL ...){
            字符串urldisplay =网址[0];
            Log.e(URL,urldisplay);
            位图mIcon11 = NULL;
            尝试{
                在的InputStream =新的java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.de codeStream(中);
            }赶上(例外五){
                Log.e(错误,e.getMessage());
                e.printStackTrace();
            }
            返回mIcon11;
        }        保护无效onPostExecute(位图结果){
            thumbnail.setImageBitmap(结果);
        }
    }}


解决方案

  ImageView的缩略图=(ImageView的)findViewById(R.id.btnThumbnail);

您需要充气的布局后能得到你的形象,否则 findViewById 将返回

  ImageView的缩略图;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.detail);
    缩略图=(ImageView的)findViewById(R.id.btnThumbnail)

I'm getting my image through an URL and trying to set that to my imageView. But I'm getting a Null Pointer Exception. It is not even entering my asynchtask.

Is there something I'm doing wrong or not seeing?

public class DetailsActivity extends Activity {
    ImageView thumbnail = (ImageView) findViewById(R.id.btnThumbnail);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);

        final Bundle extras = getIntent().getExtras();


        String img = extras.getString("Thumbnail");
        new DownloadImageTask((ImageView) findViewById(R.id.btnThumbnail))
                .execute("http://mysite.com/images/"
                        + img);


        }

    class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

        public DownloadImageTask(ImageView bmImage) {
            thumbnail = bmImage;
        }

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

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

}
解决方案
ImageView thumbnail = (ImageView) findViewById(R.id.btnThumbnail);

You need to get your image after inflating the layout, otherwise findViewById will returns null :

ImageView thumbnail;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail);
    thumbnail = (ImageView) findViewById(R.id.btnThumbnail)

这篇关于空指针异常时,从URL获取图像摆在ImageView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:30