Android 有没有办法在AsyncTask中显示AlertDialog?

Android 有没有办法在AsyncTask中显示AlertDialog?,android,android-asynctask,android-alertdialog,Android,Android Asynctask,Android Alertdialog,我想显示AsyncTask中其他类中的AlertDialog。 示例> 我想g_ctx.class.runonuiThread。。。但我不能打电话给runonuithread 如何解决它?您可以使用处理程序在主线程中调用它 new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { // Your code here

我想显示AsyncTask中其他类中的AlertDialog。 示例>

我想g_ctx.class.runonuiThread。。。但我不能打电话给runonuithread


如何解决它?

您可以使用处理程序在主线程中调用它

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
             // Your code here
        }
    });

活动类中存在runOnUiThread方法。所以你应该把活动传给你的班级。 例如:


致电Context.runonuithread.Related:谢谢!!你节省了我的时间!!!这对我很好。我只是想知道这是否是关于活动的“从不调用错误…”??cuiz我正在将Toast更改为AlertDialog。。。在自定义Toast msg=消息之前;runOnUiThreadnew Runnable{@Override public void run{View View View=View.inflateForm_Main.this,R.layout.common_toast,null;TextView tv=TextView View View.findviewbydr.id.TextView toast;tv.setTextmsg;toast t=new toast_context;t.setview;t.setDurationToast.LENGTH_LONG;t.setGravityGravity.CENTER,0;t.show;};@Adrian在这种情况下,只有当activity为null时,才会出现错误,所以您还应该在方法顶部检查activity if为null。谢谢!!这对我来说太完美了。我解决了我的问题!
    Context g_ctx;
String g_content;
public void ez_ConfirmAlertDialog(Context ctx, String content) {
    this.g_ctx=ctx;
    this.g_content=content;

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT);
            builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                        }
                    });

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            alertDialog.getWindow().setLayout(displayWidth / 2, LayoutParams.WRAP_CONTENT);
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239));

        }
    });

}
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
             // Your code here
        }
    });
public class MyClass{


public void ez_ConfirmAlertDialog(Activity activity, String content) {
    this.g_ctx=ctx;
    this.g_content=content;

    if(activity == null){
       return;
    }

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT);
            builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                        }
                    });

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            alertDialog.getWindow().setLayout(displayWidth / 2, LayoutParams.WRAP_CONTENT);
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239));

        }
    });

}

}