2 years ago
#50439
Kraken
Android app crashes when trying to append received message from USB
So when I try to append a string to my textview gotten from my callback function, the app crashes. I'm sending a message from arduino nano to my android and I can see the message on serial monitor in Ardunio IDE but when I connect the arduino to my Android through a OTG cable it just crashes. If you need more information about the code or error messages, please let me know. I dont know what to look for.
The crashing happens if I uncomment the commented line below.
private var mCallback = UsbReadCallback { data: ByteArray? ->
Log.i("Serial", "Data received: ${data.toString()}")
val dataStr = data.toString()
val textview = findViewById<TextView>(R.id.txRead)
// textview.append(dataStr)
}
And this is the logcat when the commented line is uncommented, just as the message got received.
01-18 23:06:52.244 32733 342 I Serial : Data received: [B@15e7ea4
01-18 23:06:52.258 32733 342 E AndroidRuntime: FATAL EXCEPTION: Thread-13
01-18 23:06:52.258 32733 342 E AndroidRuntime: Process: com.example.thirdattempt, PID: 32733
01-18 23:06:52.258 32733 342 E AndroidRuntime: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-18 23:06:52.258 32733 342 E AndroidRuntime: at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7361)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1191)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at android.view.ViewGroup.invalidateChild(ViewGroup.java:5261)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at android.view.View.invalidateInternal(View.java:13708)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at android.view.View.invalidate(View.java:13643)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at android.view.View.invalidate(View.java:13627)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at android.widget.TextView.checkForRelayout(TextView.java:7667)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at android.widget.TextView.setText(TextView.java:4585)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at android.widget.TextView.setText(TextView.java:4442)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at android.widget.TextView.append(TextView.java:4139)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at android.widget.TextView.append(TextView.java:4129)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at com.example.thirdattempt.MainActivity.mCallback$lambda-5(MainActivity.kt:123)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at com.example.thirdattempt.MainActivity.$r8$lambda$sDCpzleXwuhQv0Ik07ap_TtZY1w(MainActivity.kt)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at com.example.thirdattempt.MainActivity$$ExternalSyntheticLambda4.onReceivedData(Unknown Source)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at com.felhr.usbserial.UsbSerialDevice$WorkerThread.onReceivedData(UsbSerialDevice.java:391)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at com.felhr.usbserial.UsbSerialDevice$WorkerThread.doRun(UsbSerialDevice.java:360)
01-18 23:06:52.258 32733 342 E AndroidRuntime: at com.felhr.usbserial.AbstractWorkerThread.run(AbstractWorkerThread.java:21)
01-18 23:06:52.277 1199 1199 I SendBroadcastPermission: action:android.intent.action.DROPBOX_ENTRY_ADDED, mPermissionType:0
01-18 23:06:52.277 573 1002 I logserver: handle_logfile_events, Object Path:/data/system/dropbox/, mask=0x00000080
01-18 23:06:52.277 573 1002 I logserver: process_one_event, event->len=48, name=data_app_crash@1642518412277.txt
01-18 23:06:52.277 573 1002 I logserver: find_first_match: find match, plogs(type=1, pfile=/data/system/dropbox/, match=app_crash)
01-18 23:06:52.278 573 1002 I iMonitor: create: 901001001
01-18 23:06:52.278 573 1002 I logserver: process_event_by_imonitor, pos=0, type=crash, event_id=901001001
01-18 23:06:52.278 573 1002 I logserver: get_acr_info, match:(app_crash), actual_name:(data_app_crash@1642518412277.txt)
01-18 23:06:52.278 573 1002 E logserver: imonitor_add_dynamic_path, path:/data/system/dropbox/data_app_crash@1642518412277.txt
01-18 23:06:52.278 573 1002 I iMonitor: send: 901001001
01-18 23:06:52.278 573 1002 I logserver: imonitor_send_event ret=156
01-18 23:06:52.279 573 1003 I logserver: handle_exception_events enter
01-18 23:06:52.286 1199 8620 E ReportTools: This is not beta user build
01-18 23:06:52.288 32733 342 I Process : Sending signal. PID: 32733 SIG: 9
01-18 23:06:52.290 1778 2064 I ash : com.example.thirdattempt { running duration=2941 } transition to: end, reason:not_running
01-18 23:06:52.290 1778 2064 I ash : remove app record pkg: com.example.thirdattempt
01-18 23:06:52.292 1199 1212 E Process : isHighLoadProcess: get cpu failed
android
communication
nano
0 Answers
Your Answer