You specified an invalid View id here findViewById(R.layout.activity_main) . It should be something like:
findViewById(R.id.your_view_id);
Also, be sure to call setContentView(R.layout.activity_main) immediately after super.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 from Activity.onCreate , so we should use Handler.postDelayed here.
final LinearLayout layout = (LinearLayout) findViewById(R.id.layout); final AnimationDrawable drawable = new AnimationDrawable(); final Handler handler = new Handler(); drawable.addFrame(new ColorDrawable(Color.RED), 400); drawable.addFrame(new ColorDrawable(Color.GREEN), 400); drawable.setOneShot(false); layout.setBackgroundDrawable(drawable); handler.postDelayed(new Runnable() { @Override public void run() { drawable.start(); } }, 100);
source share