I have a problem with changing background color. I want to change it three seconds after the start of the activity. I have created a timer and a handler+runnable.It works fine when I want to change a special EditText after three seconds. but when I add the line layout_interact.setBackgroundColor(Color.GREEN);
and start the app in the virtual device, it crashes when it is starting the activity which contains the timer and handler.
I have also tried to start a new activity after three seconds, but that ended up with the same error.
Here's my code:
public class ActivityInteractProcess extends Activity{
final Handler handler_interact=new Handler();//not defined as final variable. may cause problem
View layout_interact=(View) findViewById(R.id.layoutintprocess);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.page_interact_process);
//creating timer
Timer timer_interact=new Timer();
timer_interact.schedule(new TimerTask() {
@Override
public void run() {UpdateGUI();}
}, 3000);
}
private void UpdateGUI() {
handler_interact.post(runnable_interact);
}
//creating runnable
final Runnable runnable_interact = new Runnable() {
public void run() {
layout_interact.setBackgroundColor(Color.GREEN); //this is the line which makes the app Force close.
}
};
}
I haven't forgot to add the following line to xml layout of the activity:
android:id="@+id/layoutintprocess"
Here are the error I got in LogCat:
AndroidRuntime(571): FATAL EXCEPTION: main
AndroidRuntime(571): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{aiLab.tests.autism/aiLab.tests.autism.ActivityInteractProcess}: java.lang.NullPointerException
AndroidRuntime(571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1739)
AndroidRuntime(571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
AndroidRuntime(571): at android.app.ActivityThread.access$500(ActivityThread.java:122)
AndroidRuntime(571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
AndroidRuntime(571): at android.os.Handler.dispatchMessage(Handler.java:99)
...
change your code to this:
public class ActivityInteractProcess extends Activity{
final Handler handler_interact=new Handler();//not defined as final variable. may cause problem
View layout_interact;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.page_interact_process);
layout_interact =(View) findViewById(R.id.layoutintprocess);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
//creating timer
Timer timer_interact=new Timer();
timer_interact.schedule(new TimerTask() {
@Override
public void run() {UpdateGUI();}
}, 3000);
}
private void UpdateGUI() {
handler_interact.post(runnable_interact);
}
//creating runnable
final Runnable runnable_interact = new Runnable() {
public void run() {
layout_interact.setBackgroundColor(Color.GREEN);
}
};
}