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.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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.