Here is what I want to do: I click on a button which opens and activity that changes the background periodically.
My code looks like this:
RelativeLayout relativeLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picscreen);
Thread background = new Thread(){
public void run(){
try {
sleep(2000);
relativeLayout = new RelativeLayout(this);
relativeLayout.setBackgroundResource(R.drawable.img10);
setContentView(relativeLayout);
}catch (Exception e){
}
}
};
background.start();
}protected void onDestroy(){
super.onDestroy();
}
The error says the relative Layout cannot ba applied to a thread. What do I do wrong?
The error says the relative Layout cannot ba applied to a thread
Because run
method context is used for creating RelativeLayout
layout object and calling setContentView
You should use YourActivityName.this
instead of this
for calling setContentView
method and creating RelativeLayout
layout object.
Suggestion:
When Application run with current code application will crash with following message:
Only the original thread that created a view hierarchy can touch its views.
So use runOnUiThread
or Handler (instead of Thread) for doing some task after specific time