imageView获取图片空指针异常

通过URL获取图片时,然后在imageView中设置,结果系统给我报出空指针异常。

请高手帮忙解决一下,谢谢。

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

}

2个回答

写法没问题,只不过thumbnail不应该在外部去查找指向他的id
ImageView thumbnail = (ImageView) findViewById(R.id.btnThumbnail);
改成:
ImageView thumbnail即可

private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imageView);
        new DownloadImageTask()
        .execute("http://imgstatic.baidu.com/img/image/shouye/liushishi1.jpg");
    }

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

        @Override
        protected void onPostExecute(Bitmap result) {
            imageView.setImageBitmap(result);
        }


        @Override
        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;
        }

    }

这样写没发现什么问题,你那写法有问题

cj6585256
cj6585256 这样成员变量ImageView 就不为null了
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
从ImageView中获取图片的大小
方法1,通过Andorid中LayoutParams来获取:           LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。              
Android简单自定义View——获取图片颜色的ImageView
自定义View从ImageView图片中取色,可用于蓝牙灯颜色的变化。
从网络获取图片并载入到ImageView
布局文件:&amp;lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;match_parent&quot; android:orientation=&quot;vertical&quot;...
空指针异常,又是空指针异常!!!
我现在做的是一个后台管理员登录的系统,但是提示有空指针异常!!异常信息如下:rnroot cause rnrnjava.lang.NullPointerExceptionrn com.tytd.argi.persistence.AdminSql.findByPwd(AdminSql.java:46)rn com.tytd.argi.business.Admin.getByPwd(Admin.java:35)rn com.tytd.argi.struts.action.AdmLogAction.execute(AdmLogAction.java:34)rn org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)rn org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)rn org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)rn org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:709)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:802)rnrnrnnote The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.rn
空指针异常
对空指针异常进行举例,并对实例进行解释,
空指针异常...
usedroom[i]=String.valueOf(rs1.getInt("roomID"));rnrn这句为什么会有NULLPOINTEREXCEPTION?
空指针异常。
[img=https://img-bbs.csdn.net/upload/201707/10/1499647873_608139.png][/img]rn这样先判断是否存在,然后不存在就插入,再查询。。 结果查询为空,整个方法报空指正异常rn[img=https://img-bbs.csdn.net/upload/201707/10/1499648092_217702.png][/img]rn如果这样,不判断是否存在,直接插入,再查询,就正常,无语,求大神指教
空指针异常,? - -#
[code=Java]rnrnimport java.awt.*;rnimport javax.swing.*;rnrnclass Astar extends JPanel rn int[][] map = rn 1,1,0,0,0,0,0,0,0,0,rn 1,0,0,0,0,0,0,0,0,0,rn 0,0,0,0,0,0,0,0,0,0,rn 0,0,0,1,1,1,1,1,0,0,rn 0,0,0,0,0,0,0,1,0,0,rn 0,0,0,0,0,0,0,1,0,0,rn 0,0,0,0,0,0,0,1,0,0,rn 1,1,1,0,0,0,0,0,0,0,rn 1,0,0,0,0,0,0,0,0,0,rn 0,0,0,0,0,0,1,1,0,0,rn ;rn Cell[][] map1;rn rn public Astar()rn map1 = new Cell[10][10];rn for(int i=0;i<10;i++)rn for(int j=0;j<10;j++)rn map1[i][j].value = map[i][j];rn JFrame f = new JFrame();rn f.setBounds(100,100,600,500);rn f.add(this);rn f.setVisible(true);rn rn public void paintComponent(Graphics g)rn for(int i=0;i<10;i++)rn for(int j=0;j<10;j++)rn if(map[i][j]==0)rn g.draw3DRect(40*j,40*i,40,40,true);rn g.drawString(map1[i][j].toString1(),40*j,40*i);rn rn else if(map[i][j]==1)rn g.fill3DRect(40*j,40*i,40,40,true); rn rn rn rn public static void main(String args[])rn new Astar(); rn rn class Cellrn int value;rn boolean seen;rn boolean open;rn int direction;rn Cell()rn seen = true;rn open = true; rn rn public String toString1()rn if(open)rn return "Open";rn else return "Closed"; rn rn rnrnrn[/code]
空指针异常??
请问空指针异常:rnjava.lang.NullPointerExceptionrn是由那些情况引起的,会带来什么后果?怎么解决呢
获取图片
请教如何获取这个url的图片数据 就是获取 Image型的数据,多谢rn[code=csharp]http://image.58.com/showphone.aspx?t=v55&v=1EE11D791C906A0AN8006F97631C8190E[/code]
安卓课程二十四 ImageView从网络上获取图片
本节采用两种方法创建Bitmap对象。主要过程是在xml中创建两个组件ImageView和Button。点击按钮下载图片,并做显示。创建了工具类HttpUtils,下载使用。 activity_main.xml &amp;lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; xmlns:tool...
相关热词 c#检测非法字符 c#双屏截图 c#中怎么关闭线程 c# 显示服务器上的图片 api嵌入窗口 c# c# 控制网页 c# encrypt c#微信网页版登录 c# login 居中 c# 考试软件