29.05.2012, 09:10. Просмотров 8990. Ответов 13
Решил создать таймер для андроида и столкнулся с некоторыми проблемами.
Приложение запускается но при активации таймера выдает ошибку.
Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| package com.prim.sample;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Prim1Activity extends Activity {
private TextView tx;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button bt=(Button)findViewById(R.id.but);
tx=(TextView)findViewById(R.id.text);
bt.setOnClickListener(new OnClickListener() {
int i = 0;
public void onClick(View arg0) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
tx.setText(Integer.toString(i++));
}
}, 0,100);
}
});
}
} |
|
и main.xml
XML |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fff"/>
</LinearLayout> |
|