简体   繁体   English

无法在未调用Looper.prepare()的线程内创建处理程序-Android Marmalade

[英]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: 您可以执行以下两项操作来解决此问题:

  • Add a Looper to the current thread. Looper添加到当前线程。
  • Make sure you're creating the 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.

相关问题 Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()Android的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() Android java.lang.RuntimeException:无法在未在Android中调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() in Android 警报对话框崩溃Android测试 - “无法在未调用Looper.prepare()的线程内创建处理程序” - Alert Dialog crashes Android test - “Can't create handler inside thread that has not called Looper.prepare()” Android上的Junit测试领域。 无法在未调用Looper.prepare()的线程内创建处理程序 - Junit Testing Realm on Android. Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()Android的线程内创建处理程序。 循环计时器 - Can't create handler inside thread that has not called Looper.prepare() Android. Loop in a timer Android空指针异常,并且无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android nullpointer exception and Can't create handler inside thread that has not called Looper.prepare() Android 2.3无法在未调用Looper.prepare()(AsyncTask)的线程内创建处理程序 - Android 2.3 Can't create handler inside thread that has not called Looper.prepare() (AsyncTask) Android:无法在未调用Looper.prepare()的线程内创建处理程序运行新线程 - Android: Can't create handler inside thread that has not called Looper.prepare() Running new threads Android:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android: Can't create handler inside thread that has not called Looper.prepare()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM