In my program I am trying to display an image from a file using an ImageView. I have declared the ImageView to be related to an ImageView XML file and have set the value to be an image from a file using BitmapFactory. After all this, I still am receiving a NullPointerException because the ImageView variable is apparently still null. If you have any ideas or advice, please let me know, thanks!
Here’s the code I am talking about:
public void imageViewMethod(String file){
Logger log = Logger.getLogger("com.example.myclass");
try {
File fileName = new File(root, file);
if(fileName.exists()){
String dirFileName = fileName.toString();
Toast.makeText(getApplicationContext(), dirFileName, Toast.LENGTH_LONG).show();
ImageView iv = (ImageView)findViewById(R.id.image);
iv.setImageBitmap(BitmapFactory.decodeFile(dirFileName));
super.setContentView(iv);
}
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Hit Exception", Toast.LENGTH_LONG).show();
log.log(Level.SEVERE, "uncaught exception", e);
}
}
And the corresponding XML file:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
</ImageView>
looks you have not set the ContentView in oncreate you have to set ContentView before getting ImageView