Java TextView.setText因线程而不工作?
我正在运行一个线程,该线程每秒数次从设备接收数据字符串。我正在尝试更新TextView以显示该数据,但是一旦我在Java TextView.setText因线程而不工作?,java,android,textview,android-broadcast,Java,Android,Textview,Android Broadcast,我正在运行一个线程,该线程每秒数次从设备接收数据字符串。我正在尝试更新TextView以显示该数据,但是一旦我在connector.run()中启动线程,我就无法设置TextView的文本。即使在run方法上方运行setText()时,它也不起作用,除非我注释掉run方法调用 这里是我调用run方法的地方 readWeight.setOnClickListener(new View.OnClickListener() { public void onClick(V
connector.run()
中启动线程,我就无法设置TextView的文本。即使在run方法上方运行setText()
时,它也不起作用,除非我注释掉run方法调用
这里是我调用run方法的地方
readWeight.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//inputWindow.setText("helloooooooo worldddddd");
connector.run();
setInputWindow();
//readWeight.setVisibility(View.INVISIBLE);
}
});
这是我在另一个类中的run方法
public void run() {
// Keep listening to the InputStream while connected
while (true) {
try {
output = "";
//read the data from socket stream
if(mmInStream != null) {
mmInStream.read(buffer);
for(byte b : buffer)
{
char c = (char) b;
if(c >=' ' && c <'z') {
// System.out.print(c);
output += c;
}
}
System.out.println();
Intent intent = new Intent();
intent.setAction("com.curie.WEIGHT_RECEIVED");
intent.putExtra("Output",output);
LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcastSync(intent);
// LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcast(intent);
}
// Send the obtained bytes to the UI Activity
} catch (IOException e) {
//an exception here marks connection loss
//send message to UI Activity
break;
}
}
}
public void run(){
//连接时继续收听InputStream
while(true){
试一试{
输出=”;
//从套接字流读取数据
如果(mmInStream!=null){
mmInStream.read(缓冲区);
for(字节b:缓冲区)
{
字符c=(字符)b;
如果(c>=''&&c=''&&c用于后台工作,请使用处理程序(使用此代码)
如果你想更新用户界面,请使用runOnUiThread()
希望这对你有帮助
你喜欢这样吗
@Override
public void run() {
// Always cancel discovery because it will slow down a connection
//Log.d("workkkkkk","$$$$$$$$$$$$$$$$****** printingggggg ******$$$$$$$$$$$$$$$$");
while (true) {
//counter++;
try {
output = "";
//read the data from socket stream
//mmInStream != null && counter%10000000 == 1
if(mmInStream != null) {
mmInStream.read(buffer);
for(byte b : buffer)
{
char c = (char) b;
if(c >=' ' && c <'z') {
// System.out.print(c);
output += c;
}
}
// context == activity context
context.runOnUiThread(new Runnable() {
@Override
public void run() {
// here do the UI chnages
// you can set text in textview Here
}
});
}
// Send the obtained bytes to the UI Activity
} catch (IOException e) {
//an exception here marks connection loss
//send message to UI Activity
break;
}
}
}
@覆盖
公开募捐{
//始终取消查找,因为它会减慢连接速度
//Log.d(“workkkkk”、“$$$$$$$$$$****打印GGGGG*********$$$”;
while(true){
//计数器++;
试一试{
输出=”;
//从套接字流读取数据
//mmInStream!=null&计数器%10000000==1
如果(mmInStream!=null){
mmInStream.read(缓冲区);
for(字节b:缓冲区)
{
字符c=(字符)b;
如果(c>=''&&csetInputWindow()
方法做什么?它只调用'inputWindow.setText(weight);'我用它来尝试延迟,看看这是否有帮助。所以inputWindow.setText(weight);
在inputWindow.setText(“Hellooo WorldDDD”)之后
将其更改为weight
您能告诉我wight的值是多少吗?当我将它打印到控制台时,我可以告诉它weight的值是多少,但当我调用setText()
时它不会出现,但即使是inputWindow.setText(“hellooooworlddddd”)
也不会工作,除非我注释掉connector.run()
你需要在工作线程中运行你的run
方法。这两个方法都可以在我的非UI类中使用吗?我如何指定要执行的活动?只需通过构造函数将活动的上下文传递到非UI类中,在上下文的帮助下,你就可以获得这些方法并完成工作。使用Have it a不是更好吗自动管理后台线程和与主(UI)线程的通信?是的,您可以根据需要使用异步任务。我将runOnUiThread放在哪里?我正在努力实现这一点。
new Handler().post(new Runnable() {
@Override
public void run() {
// here do the background task
}
});
if u want to update ui Just do this
runOnUiThread(new Runnable() {
@Override
public void run() {
// here do the UI chnages
}
});
@Override
public void run() {
// Always cancel discovery because it will slow down a connection
//Log.d("workkkkkk","$$$$$$$$$$$$$$$$****** printingggggg ******$$$$$$$$$$$$$$$$");
while (true) {
//counter++;
try {
output = "";
//read the data from socket stream
//mmInStream != null && counter%10000000 == 1
if(mmInStream != null) {
mmInStream.read(buffer);
for(byte b : buffer)
{
char c = (char) b;
if(c >=' ' && c <'z') {
// System.out.print(c);
output += c;
}
}
// context == activity context
context.runOnUiThread(new Runnable() {
@Override
public void run() {
// here do the UI chnages
// you can set text in textview Here
}
});
}
// Send the obtained bytes to the UI Activity
} catch (IOException e) {
//an exception here marks connection loss
//send message to UI Activity
break;
}
}
}