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

ArminKH

Well-Known Member
Hi
I have a code module and in that i create a panel similar to progressdialog
When user tap on connect button this panel show but when user connected to site i want remove panel
When i use a cancel button in that panel its work But when i remove that by a sub this errors accured
Exception : Only the original thread that created a view hierarchy can touch its views.

Plz help
 

ArminKH

Well-Known Member
I add panel in activity and then remove that from activity
What is wrong?
 
Upvote 0

ArminKH

Well-Known Member
Are you using the Threading library?

I test that with and without threading library
When i remove a created view in mudal by a sub from activity this error appare but when i remove that for example by a button or a timer thats ok i dont know any more but i think we can just remove a view(created by code mudal)a viewable control such as label or button
And this work in jobdone sub
 
Upvote 0

ArminKH

Well-Known Member
You cannot touch the UI from a background thread.

How can i remove my panel by standard way?
This is similar to progress dialog
For example when user tap on connect my custom progress dialog show and when user connected i want to remove that
But i cant remove that by a sub because that error appared
I dont use threading library
What is wrong erel?
We have too many library for example custom message box and...
How they do this?is there a standard way to remove a view?(created by code module when activity is running)
 
Upvote 0

Metalika

Member
You should upload a simple project to show what you are trying to do.
i know this this is a old tread but i have exactly exactly exactly same problem .
i make a simple class for a progress bar .

B4X:
Sub Class_Globals
    Private L1 As Label
    Private Back_Pnl,Dialog_Pnl As Panel
End Sub

Public Sub Initialize(Activity As Activity)
    Private Width As Int=50%x
    Private height As Int=30%x
 
    Back_Pnl.Initialize("Back_Pnl")
    Back_Pnl.Elevation=101dip
    Back_Pnl.Color=0x7D000000
         
    Dim Dialog_Color As ColorDrawable:Dialog_Color.Initialize2(Starter.color4,40,1,Starter.color1)
    Dialog_Pnl.Initialize("")
    Dialog_Pnl.Background=Dialog_Color
    Dialog_Pnl.Elevation=Back_Pnl.Elevation+1
    Back_Pnl.AddView(Dialog_Pnl,(Activity.Width/2)-(Width/2),(Activity.Height/2)-(height/2),Width,height)
 
 
    L1.Initialize("")
    L1.Text="Okkkkkkkkkk"
    L1.TextColor=Starter.Color2
    L1.TextSize=14
    L1.Typeface=Starter.IRAN_B
    L1.Gravity=Gravity.CENTER
 
    Dialog_Pnl.AddView(L1,0,2*height/3,Width,height/3)
 
    Activity.AddView(Back_Pnl,0,0,100%x,100%y)
    Back_Pnl.Visible=False
End Sub

Public Sub Show
    Back_Pnl.Visible=True
End Sub

Public Sub Hide
    Back_Pnl.Visible=False
End Sub

Private Sub Back_Pnl_Click
    Back_Pnl.Visible=False
End Sub

i use this progressbar during download several pic with Amir Retrofit lib .after i initilize and use show method of my class (progress dialog) show perfectly.
but after i use hide method in "onCompleted event sub" for disapear progress dialog this error show .(this is ok if i use hide method in a timer in current activity every thing is ok)

B4X:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
~e:    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:9014)
~e:    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1690)
~e:    at android.view.View.requestLayout(View.java:25759)
~e:    at android.view.View.requestLayout(View.java:25759)
~e:    at android.view.View.requestLayout(View.java:25759)
~e:    at android.view.View.requestLayout(View.java:25759)
~e:    at android.view.View.requestLayout(View.java:25759)
~e:    at android.view.View.setFlags(View.java:16263)
~e:    at android.view.View.setVisibility(View.java:11836)
~e:    at anywheresoftware.b4a.objects.ViewWrapper.setVisible(ViewWrapper.java:271)
~e:    at b4a.maliato.vat.loading._hide(loading.java:77)
~e:    at b4a.maliato.vat.rss_reader$ResumableSub_Activity_Create.resume(rss_reader.java:1026)
~e:    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
~e:    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
~e:    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
~e:    at aghajari.retrofit.Retrofit$5.onCompleted(Retrofit.java:1087)
~e:    at com.tamic.novate.download.DownSubscriber.onCompleted(DownSubscriber.java:62)
~e:    at rx.observers.SafeSubscriber.onCompleted(SafeSubscriber.java:79)
~e:    at rx.internal.operators.OperatorOnErrorResumeNextViaFunction$4.onCompleted(OperatorOnErrorResumeNextViaFunction.java:101)
~e:    at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:281)
~e:    at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:216)
~e:    at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230)
~e:    at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
~e:    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
~e:    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
~e:    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
~e:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
~e:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
~e:    at java.lang.Thread.run(Thread.java:923)
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top