更改ImageView可绘制的膨胀布局

时间:2016-04-03 05:58:42

标签: android file-handling android-inflate

我想更改保存到磁盘的imageview的drawable,并且可用。但是在设置drawable时,会发生NullPointerException

问题是,如何更改膨胀布局的imageview drawable [源图像是“/sdcard/myappfolder/user.png”],布局是否膨胀

这是我的代码部分:

    intilt = itilt.inflate(R.layout.pic_container, null, false);    //layout is inflated successfully
    picCheck(); // method to check file availability, working perfect, if file exists, returns 1, else returns 0;
    if(picmark == 1){
        Bitmap pch = BitmapFactory.decodeFile(file.getAbsolutePath());
        imageView = (ImageView)findViewById(R.id.contimg);  //contimg is ImageView id from layout pic_container
        imageView.setImageBitmap(pch);  
        /**
        *  Caused by: java.lang.NullPointerException
        *  at com.package.packagename.javaact.onCreate(javaact.java:83)
        */
    }
    else{
        Toast.makeText(this, "File Not Found, setting Default", Toast.LENGTH_SHORT).show();
    }

3 个答案:

答案 0 :(得分:0)

试试这样:

imageView = (ImageView) intilt .findViewById(R.id.contimg);

答案 1 :(得分:0)

//you should add it to somewhere. intilt = itilt.inflate(R.layout.pic_container,a existed View, true); 
intilt = itilt.inflate(R.layout.pic_container, null, false);    //layout is inflated successfully
    picCheck(); // method to check file availability, working perfect, if file exists, returns 1, else returns 0;
    if(picmark == 1){
        Bitmap pch = BitmapFactory.decodeFile(file.getAbsolutePath());
        //if you have inflated it into somewhere, you can just use below.
        imageView = (ImageView)intilt.findViewById(R.id.contimg);  //contimg is ImageView id from layout pic_container
        imageView.setImageBitmap(pch);  
        /**
        *  Caused by: java.lang.NullPointerException
        *  at com.package.packagename.javaact.onCreate(javaact.java:83)
        */
    }
    else{
        Toast.makeText(this, "File Not Found, setting Default", Toast.LENGTH_SHORT).show();
    }

答案 2 :(得分:0)

试试这个

intilt = itilt.inflate(R.layout.pic_container, null, false);
picCheck();
if(picmark == 1){
    Bitmap pch = BitmapFactory.decodeFile(file.getAbsolutePath());
    imageView = (ImageView) intilt.findViewById(R.id.contimg); //Change here 
    imageView.setImageBitmap(pch);  
}
else{
    Toast.makeText(this, "File Not Found, setting Default", Toast.LENGTH_SHORT).show();
}