当我在我的活动中调用这个线程时,它会使UI没有响应性,因此一旦它启动,我就无法做任何事情。我认为这与I/O流有关,但我不知道如何解决这个问题。任何帮助都会感谢,但我是新的Android和线程,所以我可能有一些后续问题。
这是我的帖子:
mHandler = new Handler();
new Thread(new Runnable() {
@Override
public void run () {
mHandler.post(new Runnable() {
@Override
public void run () {
int counter = 0;
while (true) {
counter++;
try {
output = "";
if (mInStream != null) {
mInStream.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);
if (counter % 10 == 0) {
System.out.println(counter);
LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcastSync(intent);
}
}
// Send the obtained bytes to the UI Activity
} catch (IOException e) {
//an exception here marks connection loss
//send message to UI Activity
break;
}
}
}
});
}
}).start();编辑:答案确实解决了我的问题,我现在收到了这个错误。当我调用inputwindow.setText(weight)时会发生此错误,它将TextView中的文本设置为从线程广播的值。
E/AndroidRuntime: FATAL EXCEPTION: Thread-2358
Process: com.example.curie.fairbanks_03, PID: 15718
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8358)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1364)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5433)
at android.view.View.invalidateInternal(View.java:13997)
at android.view.View.invalidate(View.java:13961)
at android.view.View.invalidate(View.java:13945)
at android.widget.TextView.checkForRelayout(TextView.java:8403)
at android.widget.TextView.setText(TextView.java:5011)
at android.widget.TextView.setText(TextView.java:4836)
at android.widget.TextView.setText(TextView.java:4811)
at com.example.curie.fairbanks_03.InputActivity$2.onReceive(InputActivity.java:67)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:311)
at android.support.v4.content.LocalBroadcastManager.sendBroadcastSync(LocalBroadcastManager.java:289)
at com.example.curie.fairbanks_03.BluetoothConnection$2.run(BluetoothConnection.java:176)
at java.lang.Thread.run(Thread.java:818)发布于 2018-06-01 13:49:31
它使您的UI没有响应,因为它在UI线程中运行!启动一个新线程来运行任务,但是在这个线程中,您将调用mHandler.post,它只是返回到创建mHandler的线程,即UI线程。没有理由这样做。只需在您创建的线程中执行操作,而不调用处理程序。
new Thread(new Runnable() {
@Override
public void run () {
int counter = 0;
while (true) {
counter++;
try {
output = "";
if (mInStream != null) {
mInStream.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);
if (counter % 10 == 0) {
System.out.println(counter);
LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcastSync(intent);
}
}
// Send the obtained bytes to the UI Activity
} catch (IOException e) {
//an exception here marks connection loss
//send message to UI Activity
break;
}
}
}
}).start();https://stackoverflow.com/questions/50644743
复制相似问题