开发者

Android SpeechRecognizer should only be used from the application's main thread

I am trying to integrate some of Androids Speech APIS in my AndEngine based game.

I placed my code within the BaseGame activity - however this error appears at runtime:

05-06 23:51:28.955: ERROR/AndroidRuntime(553): java.lang.RuntimeException: SpeechRecognizer should be used only from the application's main thread

How do I access the ap开发者_高级运维plications main thread? And how can I ensure my Speech code runs in it.

The setup is as follows:

Main Class - BaseGameActivity SpeechClass

Main Class instantiates a new SpeechClass + calls its method - then I get the above error.

Please advise on how I can do this correctly.


Call the SpeechClass on the main thread by doing something like this:

final SpeechClass c = getSpeechClass(); // get the speech class

View v = findViewById(R.id.anyview); //fetch a View: any one will do

v.post(new Runnable(){ public void run(){ c.doSomething(); }});

That should run it on the main thread. Might slow your UI down if doSomething takes a while, but you get to work that out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜