2014-09-03 65 views
1

我在更改背景顏色時遇到問題。我想在活動開始後三秒鐘更改它。我創建了一個計時器和一個處理程序+ runnable.It在三秒鐘後想要更改特殊的EditText時正常工作。但是當我添加行layout_interact.setBackgroundColor(Color.GREEN);並在虛擬設備中啓動應用程序時,它會在啓動包含計時器和處理程序的活動時崩潰。 我也嘗試過三秒鐘後開始一項新活動,但最終發生同樣的錯誤。 這裏是我的代碼:Android - 在特定時間後更改背景顏色

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) 
... 

回答

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); 
    } 
}; 
} 
+0

它適用於我,如果你想我可以把結果圖片。也許你的問題在代碼 – MHP 2014-09-03 20:19:41

+0

的其他部分謝謝。我嘗試了幾次,它的工作。我的安裝問題不時發生。我不知道爲什麼。 – 0saber0 2014-09-03 20:46:59