我正在开发一个Android应用程序,通过蓝牙连续从raspberry pi 2接收字符串。
代码运行良好,但只有begintoListentoData无效..
from time import sleep
import serial
ser = serial.Serial(port='/dev/serial0', boudrate=115200)
while True:
ser.write("project ")
python代码在这里:
04-24 15:52:41.591 3991-6131/com.first.majazen.appfinal E/AndroidRuntime: FATAL EXCEPTION: Thread-65679
Process: com.first.majazen.appfinal, PID: 3991
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8251)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1307)
at android.view.View.requestLayout(View.java:20148)
at android.view.View.requestLayout(View.java:20148)
at android.view.View.requestLayout(View.java:20148)
at android.view.View.requestLayout(View.java:20148)
at android.view.View.requestLayout(View.java:20148)
at android.widget.TextView.checkForResize(TextView.java:8357)
at android.widget.TextView.spanChange(TextView.java:9404)
at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:12067)
at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:1057)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:763)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:662)
at android.text.Selection.setSelection(Selection.java:125)
at android.text.Selection.setSelection(Selection.java:134)
at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:325)
at android.widget.TextView.setText(TextView.java:4976)
at android.widget.TextView.setText(TextView.java:4834)
at android.widget.EditText.setText(EditText.java:117)
at android.widget.TextView.setText(TextView.java:4809)
at com.first.majazen.appfinal.Recieve$1.run(Recieve.java:99)
at java.lang.Thread.run(Thread.java:818)
这是我有的logcat
请帮我解决这个问题!
TIdMultiPartFormDataStream
答案 0 :(得分:0)
第99行,替换
text.setText(data);
通过
Recieve.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
text.setText(data);
}
});
如错误日志中所述,您尝试从GUI线程以外的其他线程修改GUI。那是被禁止的。 此外,尝试将此代码段封装到另一种方法中以提高可读性。