Android Question Archiver: Only the original thread that created a view hierarchy can touch its views.

Alessandra Pellegri

Active Member
Licensed User
I have some problem with Archiver library and I don't understand why.

B4X:
Sub globals
   Private LblPercentuale As Label
end sub
...
sub mySubroutine
     Dim arc As Archiver
     arc.AsyncUnZip(Utili.GetPath(NomeFile),Utili.GetFileName(NomeFile),sDBLocation,"unzip")
end Sub
...
...

Sub unzip_UnZipProgression(Count As Int, FileName As String)
   'Msgbox(Count,"")
   LblPercentuale.Text=Count
End Sub

When generates the event unzip_UnZipProgression I have this error:
B4X:
main_unzip_unzipprogression (java line: 4904)
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

If I uncomment Msgbox(Count,"") the error becomes :
B4X:
main_unzip_unzipprogression (java line: 4904)
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Could you help me please ?
Thank you
 

Roycefer

Well-Known Member
Licensed User
The reason this is happening is that arc.AsyncUnZip raises its events in a non-main thread. You can't access UI elements outside of a non-main thread (well, technically, the thread that created the UI elements). You have to use Thread.RunOnGuiThread() to run methods that access UI elements from a non-main thread.

It's not a great idea to unzip files synchronously as this will block the main thread. If you are unzipping large files, the UI will become unresponsive and you will get an ANR and the OS might kill your process. It's better to use the asynchronous version and then use Thread.RunOnGuiThread() to update the UI.
 
Upvote 0
Top