如何在线程android中显示一个对话框

时间:2014-06-05 06:19:55

标签: android multithreading dialog

我想在我的帖子中发生一些事情之后显示一个对话框

我在ui线程上运行此代码,并且runexception抛出!!

AlertDialog.Builder dlgAlert = new AlertDialog.Builder(context);

    dlgAlert.setMessage(context.getString(R.string.custom_dialog_values_new_high_score));
    dlgAlert.setTitle(context.getString(R.string.custom_dialog_values_new_high_score_title));

    final AlertDialog alertDialog = dlgAlert.create();

    dlgAlert.setPositiveButton(context.getString(R.string.custom_dialog_values_yes_button_text), new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface arg0, int arg1)
        {
            alertDialog.dismiss();
        }
    });

    dlgAlert.setCancelable(true);

    alertDialog.show();

以下例外情况是: “执行停止未恢复的活动”

问题是什么?

我该如何解决这个问题?

编辑:

我的主题部分是:

runOnUiThread(new Runnable()
    {
        public void run()
        {
            try
            {
                                    // it's the function which called dialogbox
                gameIsFinish();
            }
            catch (Exception e)
            {
            }
        }
    });

Logcat异常:

06-05 10:30:02.643: E/ActivityThread(24497): Performing stop of activity that is not resumed: {com.foroutannezhad.colorname/com.foroutannezhad.colorname.MainActivity}
06-05 10:30:02.643: E/ActivityThread(24497): java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.foroutannezhad.colorname/com.foroutannezhad.colorname.MainActivity}
06-05 10:30:02.643: E/ActivityThread(24497):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3133)
06-05 10:30:02.643: E/ActivityThread(24497):    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3220)
06-05 10:30:02.643: E/ActivityThread(24497):    at android.app.ActivityThread.access$1100(ActivityThread.java:135)
06-05 10:30:02.643: E/ActivityThread(24497):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
06-05 10:30:02.643: E/ActivityThread(24497):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-05 10:30:02.643: E/ActivityThread(24497):    at android.os.Looper.loop(Looper.java:136)
06-05 10:30:02.643: E/ActivityThread(24497):    at android.app.ActivityThread.main(ActivityThread.java:5001)
06-05 10:30:02.643: E/ActivityThread(24497):    at java.lang.reflect.Method.invokeNative(Native Method)
06-05 10:30:02.643: E/ActivityThread(24497):    at java.lang.reflect.Method.invoke(Method.java:515)
06-05 10:30:02.643: E/ActivityThread(24497):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-05 10:30:02.643: E/ActivityThread(24497):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-05 10:30:02.643: E/ActivityThread(24497):    at dalvik.system.NativeStart.main(Native Method)

提前致谢

1 个答案:

答案 0 :(得分:0)

((Activity)context).runOnUiThread(new Runnable() {

    AlertDialog.Builder dlgAlert = new AlertDialog.Builder(context);

    dlgAlert.setMessage(context.getString(R.string.custom_dialog_values_new_high_score));
    dlgAlert.setTitle(context.getString(R.string.custom_dialog_values_new_high_score_title));

    final AlertDialog alertDialog = dlgAlert.create();

    dlgAlert.setPositiveButton(context.getString(R.string.custom_dialog_values_yes_button_text), new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface arg0, int arg1)
        {
            alertDialog.dismiss();
        }
    });

    dlgAlert.setCancelable(true);

    alertDialog.show();
});