[英]Can't create handler inside thread that has not called Looper.prepare() - Android Marmalade
I'm trying to add USB controller support to my Android game. 我正在尝试将USB控制器支持添加到我的Android游戏中。 I'm using Marmalade and I've created an extension based on the USB example code.
我正在使用Marmalade,并且已基于USB示例代码创建了扩展。 Here it is:
这里是:
public class GameControllerInput extends Activity
implements InputManager.InputDeviceListener
{
private static final String TAG = "GameControllerInput";
private InputManager mInputManager;
private SparseArray<InputDeviceState> mInputDeviceStates;
private static int numEvents = 0;
public int EDK_GameControllerInput_Init()
{
LoaderActivity.m_Activity.runOnUiThread(new Runnable()
{
public void run()
{
Log.i(TAG, "Running 1 =========================");
}
});
Log.i(TAG, "Init 2 =========================");
return 1;
When I call the init function I get this error: 当我调用init函数时,出现以下错误:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
I've read other threads with this error and they say the solution is to add the LoaderActivity.m_Activity.runOnUiThread(new Runnable()
code. However, as you can see, adding this just gives me the same error. 我读过其他有此错误的线程,他们说解决方案是添加
LoaderActivity.m_Activity.runOnUiThread(new Runnable()
代码。但是,如您所见,添加LoaderActivity.m_Activity.runOnUiThread(new Runnable()
会给我同样的错误。
I'm not experienced with Java and I'm at a loss on how to fix this. 我没有Java经验,而且对如何解决这个问题一无所知。 Any help would be greatly appreciated.
任何帮助将不胜感激。
Cheers, Steve 干杯,史蒂夫
A Looper
(a message queue processor) is tied to a single thread, each thread has at most one looper. Looper
(消息队列处理器)绑定到单个线程,每个线程最多具有一个Looper
。 A Handler
needs to register itself with a Looper
to work, so each time you invoke new Handler()
, it tries to get the Looper
for the current thread (the thread that's creating the Handler
), which can be either present or not. Handler
需要向Looper
注册才能工作,因此,每次调用new Handler()
,它都会尝试获取当前线程(正在创建Handler
的线程)的Looper
,该线程可以存在也可以不存在。 The exception that you see is thrown because the thread that's creating the handler does not have a looper. 因为创建处理程序的线程没有循环程序,所以引发了您看到的异常。
There is one two things that you can do to fix this: 您可以执行以下两项操作来解决此问题:
Looper
to the current thread. Looper
添加到当前线程。 Handler
on a thread that already has a Looper
. Looper
的线程上创建Handler
。 In almost all cases, the handler is used to communicate from a background thread to the UI thread, I'm assuming that's the case here. 在几乎所有情况下,处理程序都用于从后台线程与UI线程进行通信,我假设情况就是如此。 That means option 2. Your
runOnUiThread(Runnable)
thing is close, but no cigar, because all it does is write to the log file. 这意味着选项2。您的
runOnUiThread(Runnable)
内容很近,但是没有雪茄,因为它所做的全部就是写入日志文件。
You need to move the code that creates the new Handler()
(not shown in your posted code sample) into the runOnUiThread
block, or use some other way to get it to run on the UI thread. 您需要将创建
new Handler()
的代码(未在发布的代码示例中显示)移动到runOnUiThread
块中,或使用其他方法使其在UI线程上运行。 The typical way to do this is to create it in the onCreate(Bundle)
method of your activity or fragment. 执行此操作的典型方法是在您的活动或片段的
onCreate(Bundle)
方法中创建它。
Keep in mind that, depending on your initialization order, this may mean it's initially null
as seen by your background thread, so the background code will have to be able to deal with that. 请记住,根据您的初始化顺序,这可能意味着它最初是
null
如您的后台线程所见),因此后台代码必须能够处理该问题。
最好有一个回调方法,仅在s4e
文件中的方法声明之后调用run_on_os_thread
,将其标记为主线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.