android Thread Textviev写入问题
我想通过线程编写文本视图一个简单的单词,但它是强制关闭
代码是:
package thread.denemesi;
import java.util.ArrayList;
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;
import android.widget.Toast;
public class baslat extends Activity {
Button buton1,buton2;
Thread thread2,thread1;
TextView yazi;
int sayi=0;
ArrayList<Integer> dizi;
int sayac=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buton1=(Button)findViewById(R.id.button1);
buton2=(Button)findViewById(R.id.button2);
yazi=(TextView)findViewById(R.id.textview);
dizi=new ArrayList<Integer>();
buton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
thread1=new Thread(new Runnable() {
@Override
public void run() {
// for (int i = 0; i < 100; i++) {
// dizi.add(i);
// }
for (int i = 0; i < 10; i++) {
sayac++;
try {
yazi.setText("i want to write a word");
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
thread1.start();
}
});
}
İ want to write textview a simple word via Thread but it is force close
codes is that :
package thread.denemesi;
import java.util.ArrayList;
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;
import android.widget.Toast;
public class baslat extends Activity {
Button buton1,buton2;
Thread thread2,thread1;
TextView yazi;
int sayi=0;
ArrayList<Integer> dizi;
int sayac=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buton1=(Button)findViewById(R.id.button1);
buton2=(Button)findViewById(R.id.button2);
yazi=(TextView)findViewById(R.id.textview);
dizi=new ArrayList<Integer>();
buton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
thread1=new Thread(new Runnable() {
@Override
public void run() {
// for (int i = 0; i < 100; i++) {
// dizi.add(i);
// }
for (int i = 0; i < 10; i++) {
sayac++;
try {
yazi.setText("i want to write a word");
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
thread1.start();
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该使用
adb logcat
、DDMS 或 Eclipse 中的 DDMS 透视图来检查 LogCat 并查看与“强制关闭”相关的堆栈跟踪。您会发现您的异常类似于“尝试从非 UI 线程修改 UI”。
您需要从主应用程序线程而不是后台线程调用
setText()
。您可以通过以下方式执行此操作:TextView
上调用post()
的Handler
Runnable
>runOnUiThread()
在您的Activity
上,传递一个Runnable
First, you should have used
adb logcat
, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look at the stack trace associated with your "force close".You would find that your exception is something like "Attempted to modify the UI from a non-UI thread".
You will need to call
setText()
from the main application thread, not your background thread. You can do this via:Handler
post()
on yourTextView
, passing aRunnable
runOnUiThread()
on yourActivity
, passing aRunnable