我的目的很簡單,我想創建一個從10到1計數的倒計時。我嘗試使用谷歌給出的倒計時,但我不能使它成爲一個線程,所以我用這種方式來創建相同的功能,但我有這個code.My應用程序崩潰時,我使用這個線程code.Please幫助我的人。我的應用程序崩潰時,我使用此線程代碼
public class MainActivity extends Activity {
TextView textView;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
String string = textView.getText().toString();
int num = Integer.parseInt(string);
num = num-1;
string = Integer.toString(num);
textView.setText(string);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Thread myThread = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 10; i>0;i--){
try {
Thread.sleep(1000);
//handler.sendMessage(handler.obtainMessage());
handler.sendMessage(handler.obtainMessage());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
myThread.start();
}
}
這裏是鏈接我的日誌貓,https://photos.google.com/share/AF1QipO-kaTwmY54c70Mk0K67h9sAwA77qLYjoTxClh2jxpUj9uJGeBczYkhvU_TkInLsw/ photo/AF1QipM0O6Q33D-l0x8We0RWskSk_Tt5iNr5iD84cr8T?key = bWZER2lkTkZoNDNYTjJxLXdsYS1pWFFnMzU1WUZR – clicker
您應該將您的實際logcat消息粘貼到您的帖子中。沒有在評論中添加鏈接 – codeMagic
確定兄弟,我得到10個聲望 – clicker