Maruf
Maruf

Reputation: 19

Exception while changing Background Image of Activity Runtime

I am using the following lines of code to change the background image of my android activity at runtime : private View mainLayout; //global variable
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.
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??

Upvotes: 0

Views: 52

Answers (1)

laalto
laalto

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

Related Questions