0

Libgdx ゲーム エンジンを使用する Android ゲームがあります。Libgdx の AndroidApplication クラスを拡張する Android アクティビティ (mAndroidLauncher) があります。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 Developer Console でクラッシュが発生しました。

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 がルーパーを使用してスレッドから構築されていることを確認するために、他に何かする必要がありますか?

編集: CommonsWare に感謝します。エラーは確かにGoogle Play Servicesから来ています. 具体的には、runOnUiThread() にラップされていない gameHelper.beginUserInitiatedSignIn() への呼び出しがありました。奇妙なことに、これはすべての電話でエラーを引き起こしませんでした

4

2 に答える 2

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);
于 2016-12-06T05:49:17.033 に答える