Java Question Any help with this message please ?

dealsmonkey

Active Member
Licensed User
Longtime User
Hi All,

Just got my first library working that is a wrapper for the asmack xmpp class library.

All is working fine, connects etc.

However

I am getting this log message when I try to open a toastmessage in an event raised..

The event is working fine otherwise !

B4X:
ToastMessageShow(body, False)
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
   at android.os.Handler.<init>(Handler.java:121)
   at android.widget.Toast.<init>(Toast.java:68)
   at android.widget.Toast.makeText(Toast.java:231)
   at anywheresoftware.b4a.keywords.Common.ToastMessageShow(Common.java:350)
   at com.exg.Smack.main._xmpp_packetreceived(main.java:294)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:104)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:88)
   at nejolaSmack.nejolaSmackClass$1.processPacket(nejolaSmackClass.java:122)
   at org.jivesoftware.smack.PacketReader$ListenerWrapper.notifyListener(PacketReader.java:819)
   at org.jivesoftware.smack.PacketReader$ListenerNotification.run(PacketReader.java:799)
   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
   at java.lang.Thread.run(Thread.java:1102)
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()


Any thoughts please ?

Thanks Neil


Edit : Next time I'll think a bit more before posting !! Just adder the looper.prepare() to my source code and now is sorted !! :sign0104:
 
Last edited:

agraham

Expert
Licensed User
Longtime User
It looks like you are trying to raise a Toast from a thread other than the main UI thread. That thread does not have a message queue which is the reason for the exception you see as without a message queue it can't pump messages. In addition only the main thread can interact with the UI and you will usually get a force close if you try. In Basic4android you can raise an event on the UI thread from a different thread if you need to by calling BA.raiseEventFromDifferentThread.
 

dealsmonkey

Active Member
Licensed User
Longtime User

Thanks for that info agraham. I thought I had it sorted, but was a little to quick !

Neil
 

dealsmonkey

Active Member
Licensed User
Longtime User
Agraham,

Thankyou a lot for your unselfish help. I have now got this working.

:sign0188:
 

agraham

Expert
Licensed User
Longtime User
While writing this I see that you have got it going - I hope using raiseEventFromDifferentThread. Here's what I was going to say anyway for some background info.

Just adder the looper.prepare() to my source code and now is sorted!
I'm not entirely sure that that is the correct solution. As you were already using raiseEvent then substituting raiseEventFromDifferentThread for it would probably be safer. In fact if all you have done is add a "Looper.prepare" line to the code then I'm a bit surprised you can raise a Toast at all.

Threads with Loopers are really intended to have a message Handler with a handleMessage method and then run Looper.loop to receive and process messages. This is intended to make interthread communication easier using custom messages and Runnables.
 

dealsmonkey

Active Member
Licensed User
Longtime User
Thanks, I'll have a read.

Indeed using raiseEventFromDifferentThread has solved my problem, the app now gets the message from the server using a service module

 

Shameem

Member
Licensed User
Longtime User
Smack Library

Hi

Dealsmonkey, Could you produce the Library for other users

Thanks in Advance
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…