为什么runOnUIThread仍会导致looper.prepare()错误消息

时间:2016-12-06 00:23:43

标签: java android libgdx android-alertdialog android-runonuithread

我有一款使用Libgdx游戏引擎的Android游戏。我有一个Android活动(mAndroidLauncher),它扩展了Libgdx的AndroidApplication类。有一种方法可以创建Android警报对话框:

mAndroidLauncher.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mAndroidLauncher.getContext());
        builder.setTitle("Notice");
        builder.setMessage("Alert!!!");
        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
               //OK
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
});

我在Google Play开发者控制台中发生了崩溃,如下所示:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:208)
at android.os.Handler.<init>(Handler.java:122)
at android.app.Dialog.<init>(Dialog.java:109)
at android.app.AlertDialog.<init>(AlertDialog.java:114)
at android.app.AlertDialog$Builder.create(AlertDialog.java:931)
at com.google.android.gms.common.e.a(Unknown Source)
at com.google.android.gms.common.e.a(Unknown Source)
at com.my.game.l.a(Unknown Source)
at com.my.game.l.g(Unknown Source)
at com.my.game.l.c(Unknown Source)
at com.my.game.a.b(Unknown Source)
at com.my.game.b.f.a(Unknown Source)
at com.my.game.q.b(Unknown Source)
at com.badlogic.gdx.backends.android.i.onDrawFrame(Unknown Source)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1557)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1263)

这是我的应用程序中唯一使用AlertDialog的地方,这就是为什么我确信这是导致崩溃的方法。为什么runOnUiThread会导致此错误?我是否还需要做其他事情以确保AlertDialog是使用带有looper的线程构建的?

编辑:感谢CommonsWare。该错误确实来自Google Play服务。具体来说,我调用了gameHelper.beginUserInitiatedSignIn(),它没有包含在runOnUiThread()中。虽然奇怪的是,这并没有导致所有手机出错

2 个答案:

答案 0 :(得分:0)

 new Handler(Looper.getMainLooper()).postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    //Do something here
                    AlertDialog.Builder builder = new AlertDialog.Builder(mAndroidLauncher.getContext());
        builder.setTitle("Notice");
        builder.setMessage("Alert!!!");
        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
               //OK
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
                }
            }, 0);

答案 1 :(得分:0)

你不能直接使用Activity名称调用runOnUIThread(){},如调用静态方法...在mAndroidLauncher中声明一个对象

public static Activity acitivity = this;

并将runOnUIThread调用为

mAndroidLauncher.activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mAndroidLauncher.getContext());
        builder.setTitle("Notice");
        builder.setMessage("Alert!!!");
        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
               //OK
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
});