Reputation: 3
I want to change the drawable of imageview, that is saved to disk,, and is available. but when setting drawable, a NullPointerException occurs
The question is, how to change imageview drawable of inflated layout[Source image is "/sdcard/myappfolder/user.png"], the layout is inflated
Here's my part of code:
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();
}
Upvotes: 0
Views: 483
Reputation: 6251
//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();
}
Upvotes: 0
Reputation: 116
Try this
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();
}
Upvotes: 0
Reputation: 6791
Try like this:
imageView = (ImageView) intilt .findViewById(R.id.contimg);
Upvotes: 1