I have a LinearLayout
with a few Buttons
and TextViews
. I want my background to flash at timed intervals, say from red to white to red and so on. Right now, I am trying this code, but it gives me a null pointer exception.
LinearLayout ll = (LinearLayout) findViewById(R.layout.activity_main);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50);
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
ll.startAnimation(anim); // shows null pointer exception at this line
Please help me where am I going wrong?
Try this
and If
activity_main
is your XML file name thenand use your layout id here
You have specified the wrong
View
id herefindViewById(R.layout.activity_main)
. It should be something like:Also, make sure to call
setContentView(R.layout.activity_main)
right aftersuper.onCreate
EDIT:
Here is the code that allows you to change only the background color with any colors you want. It looks like
AnimationDrawable.start()
doesn't work if called fromActivity.onCreate
, so we have to useHandler.postDelayed
here.