我有一个与串口通信的程序。我已经用线程写了
public void beginListenForData()
{
//final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
//byte b = packetBytes[i];
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(packetBytes, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
Volt.setText(data);
}
});
}
}//End of if
}
catch (IOException ex)
{
stopWorker = true;
}
}//End of while
}//End of run
});//End of workerThread
workerThread.start();
}//End of function begin Listen For Data
但是在我的程序中,run方法无效。我首先尝试在显示数据之前显示一个字母'k'。但是我在run方法中写的任何内容都没有做任何事情。在编写线程之前是否有任何要求应该添加到清单文件中?我甚至不知道我的while循环是否有效。
这是我的logcat:
12-20 10:11:09.110: D/dalvikvm(4777): GC_CONCURRENT freed 207K, 5% free 6724K/7047K, paused 4ms+8ms
12-20 10:13:59.410: W/dalvikvm(4777): threadid=11: thread exiting with uncaught exception (group=0x40a401f8)
12-20 10:13:59.450: E/AndroidRuntime(4777): FATAL EXCEPTION: Thread-184
12-20 10:13:59.450: E/AndroidRuntime(4777): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:722)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:771)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4005)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.view.View.invalidate(View.java:8515)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.widget.TextView.invalidateRegion(TextView.java:4383)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.widget.TextView.invalidateCursor(TextView.java:4325)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.widget.TextView.spanChange(TextView.java:7725)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:8074)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:909)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:614)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:520)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.text.Selection.setSelection(Selection.java:76)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.text.Selection.setSelection(Selection.java:87)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:304)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.widget.TextView.setText(TextView.java:3294)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.widget.TextView.setText(TextView.java:3162)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.widget.EditText.setText(EditText.java:78)
12-20 10:13:59.450: E/AndroidRuntime(4777): at android.widget.TextView.setText(TextView.java:3137)
12-20 10:13:59.450: E/AndroidRuntime(4777): at com.example.demo.MainActivity$6.run(MainActivity.java:505)
12-20 10:13:59.450: E/AndroidRuntime(4777): at java.lang.Thread.run(Thread.java:856)
12-20 10:14:00.640: D/OpenGLRenderer(4777): Flushing caches (mode 0)
12-20 10:14:01.560: D/OpenGLRenderer(4777): Flushing caches (mode 1)
12-20 10:14:01.620: E/ActivityThread(4777): Activity com.example.demo.MainActivity has leaked IntentReceiver com.example.demo.MainActivity$4@410bc160 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-20 10:14:01.620: E/ActivityThread(4777): android.app.IntentReceiverLeaked: Activity com.example.demo.MainActivity has leaked IntentReceiver com.example.demo.MainActivity$4@410bc160 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1043)
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1030)
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1024)
12-20 10:14:01.620: E/ActivityThread(4777): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:341)
12-20 10:14:01.620: E/ActivityThread(4777): at com.example.demo.MainActivity.searchDevices(MainActivity.java:230)
12-20 10:14:01.620: E/ActivityThread(4777): at com.example.demo.MainActivity.onCreate(MainActivity.java:93)
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.Activity.performCreate(Activity.java:4465)
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-20 10:14:01.620: E/ActivityThread(4777): at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 10:14:01.620: E/ActivityThread(4777): at android.os.Looper.loop(Looper.java:137)
12-20 10:14:01.620: E/ActivityThread(4777): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-20 10:14:01.620: E/ActivityThread(4777): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 10:14:01.620: E/ActivityThread(4777): at java.lang.reflect.Method.invoke(Method.java:511)
12-20 10:14:01.620: E/ActivityThread(4777): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-20 10:14:01.620: E/ActivityThread(4777): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-20 10:14:01.620: E/ActivityThread(4777): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
Volt.setText("Bluetooth Opened");
在线程上更新ui是错误的。需要在ui线程上更新ui。可能你的应用程序崩溃了。
答案 1 :(得分:1)
您无法在线程中更新UI,因此此行Volt.setText("Bluetooth Opened");
会给您错误