Reputation: 19
I am using the following lines of code to change the background image of my android activity at runtime :
private View mainLayout; //global variable
but I get a fatal error null pointer exception on my device when setBackgroundResource function is called. Can anyone please suggest a reason for failure??
mainLayout = findViewById(R.layout.activity_breath); // getting the layout in onCreate method
mainLayout.setBackgroundResource(R.drawable.image); //this line is called in run method of timer.
Upvotes: 0
Views: 52
Reputation: 152817
Attempting to find a R.layout
resource with findViewById()
will always fail and return null
. Use a R.id
resource identifier that exists within your activity's view hierarchy.
Upvotes: 1