我在更改背景颜色时遇到问题。我想在活动开始后三秒钟更改它。我已经创建了一个计时器和一个处理程序+ runnable。当我想在三秒钟后更改一个特殊的EditText时它工作正常。但是当我添加行layout_interact.setBackgroundColor(Color.GREEN);
并在虚拟设备中启动应用程序时,它会在启动包含计时器和处理程序的活动时崩溃。
我还尝试在三秒钟后开始一项新活动,但结果却出现了同样的错误。
这是我的代码:
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.
}
};
}
我还没有忘记将以下行添加到活动的xml布局中:
android:id="@+id/layoutintprocess"
以下是我在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)
...
答案 0 :(得分:3)
将您的代码更改为:
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);
}
};
}
答案 1 :(得分:1)
根据此article,您应使用 Handler 而不是 TaskTimer ,如下面的代码。
private void updateGUI() {
handler_interact.postDelayed(runnable_interact, 3000);
}
//creating runnable
final Runnable runnable_interact = new Runnable() {
public void run() {
layout_interact.setBackgroundColor(Color.GREEN);
}
};