首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在imageview中设置位图时的NullPointerException

在imageview中设置位图时的NullPointerException
EN

Stack Overflow用户
提问于 2011-03-14 23:07:11
回答 1查看 3.2K关注 0票数 1

嗨,所有我得到一个NPE当设置一个ImageView的位图。位图已从字节数组解码,并且不为null或空。有什么想法吗?谢谢

代码语言:javascript
复制
public class LoadPic extends Activity{

    private static final String TAG = "Loadpic";
    private ImageView imageview;
    private File tempFile;
    private byte[] imageArray;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Log.e(TAG, " loadpic onCreate");
        setContentView(R.layout.load);

        imageview = (ImageView)findViewById(R.id.imageView1);

        tempFile = new File(Environment.getExternalStorageDirectory().
             getAbsolutePath() + "/"+"image.jpg");

        imageArray = new byte[(int)tempFile.length()];

  try{

        InputStream is = new FileInputStream(tempFile);
        BufferedInputStream bis = new BufferedInputStream(is);
        DataInputStream dis = new DataInputStream(bis);


        int i = 0;

        while (dis.available() > 0) {
        imageArray[i] = dis.readByte();
        i++;
        }

        dis.close();

  } catch (Exception e) {

           e.printStackTrace();
        }

        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inSampleSize = 5;
        Bitmap bm = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo);
        Log.e(TAG, bm.toString());
        imageview.setImageBitmap(bm);

    }// end of onCreate


}//end of Activity








<?xml version="1.0" encoding="UTF-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical" android:layout_width="match_parent" 
                    android:layout_height="wrap_content" android:gravity="fill">


        <ImageView android:id="@+id/imageView1" 
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"></ImageView>


     </RelativeLayout>               
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-14 23:12:47

确保带有android:id="@+id/imageView1"ImageView实际出现在res/layout/load.xml中。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5300456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档