基本上,我試圖運行一個秒計數器和一個計數器。每10秒我想要++級別。
但這還沒有實現,到目前爲止,我只是想獲得秒顯示,但我得到運行時異常和崩潰。
谷歌搜索我看到它,因爲我試圖從我的線程更新UI,這是不允許的。 所以我想我會需要asyncTask,但我不知道如何用我的簡單的小程序做到這一點。請幫助或者給我一些替代品...Android,創建一個簡單的線程,將更新我的秒計數器
package com.ryan1;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class main extends Activity {
int level = 1;
int seconds_running=0;
TextView the_seconds;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
the_seconds = (TextView) findViewById(R.id.textview_seconds);
Thread thread1 = new Thread(){
public void run(){
try {
sleep(1000); Log.d("RYAN", " RYAN ");
updated_secs();
} catch (Exception e) {
e.printStackTrace();
Log.d("RYAN", " "+e);
}
}
};
thread1.start();
}
public void updated_secs(){
seconds_running++;
the_seconds.setText(" "+seconds_running);
}
}
嘿!我們分享同一個名字:)請問你能給我一些示例代碼嗎?在這之後的幾個小時裏,我感覺腦子已經死了...... – Ryan 2011-03-20 22:57:06
謝謝!會試試看! – Ryan 2011-03-21 00:08:28