Android Can';在DoInBackground中不显示AlertDialog

Android Can';在DoInBackground中不显示AlertDialog,android,android-alertdialog,Android,Android Alertdialog,我正在尝试使用Android中的AlertDialog,它会在检查互联网连接后通知用户他们正在脱机模式下运行 我使用了以下代码: protected Void doInBackground(Void... params) { if (this.isOnline()) { new GetJson().execute(); } else { AlertDialog.Builder builder

我正在尝试使用Android中的AlertDialog,它会在检查互联网连接后通知用户他们正在脱机模式下运行

我使用了以下代码:

protected Void doInBackground(Void... params) {
            if (this.isOnline()) {
                    new GetJson().execute();
            } else {

        AlertDialog.Builder builder1 = new AlertDialog.Builder(homeFragment);
        builder1.setMessage("INTERNET CONNECTION NOT AVAILABLE. Now you are viewing the news in Offline Mode.");
        builder1.setCancelable(true);

        AlertDialog alert1 = builder1.create();
        alert1.show();

        try {
            saveFile = new SaveIntoFile(fileName);
            jsonStr = saveFile.read();
            // Log.d(TAG,"offline data reading from a file");
            if (!jsonStr.equals(null))
                new GetDatas().execute();
            else {

            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
    return null;

}
但我在为AlertDialog添加代码时出错。如果没有AlertDialog的代码,应用程序可以正常工作。
这段代码可能有什么错误,我如何纠正它才能正常工作???

doInBackground()在主线程以外的单独线程上运行。您只能在主线程上使用UI元素。尝试在doInBackground()中使用runOnUiThread()方法来显示对话框。

此处AlertDialog将不起作用,您可以使用 System.out.println(“一些文本”); 如果您只是出于测试目的使用警报。 否则,您必须设置一个参数,以及您想要放入alertbox中的任何文本,并在主线程中使用它


如果您有任何疑问,请告诉我。

在Android中,您只能在主线程中执行UI操作。所以您可以在onPostExecute方法中显示对话框。例如:

    class anyClassName extends AsyncTask<String,String,String>{

    /* uses worker thread */
    protected String doInBackground(Void... params) {
                if (this.isOnline()) {
                        new GetJson().execute();
                } else { return null }
// ... rest of your code
return ""
    }

    /* uses main thread*/
    protected void onPostExecute(String result){
    if(result == null ){
    AlertDialog.Builder builder1 = new AlertDialog.Builder(homeFragment);
            builder1.setMessage("INTERNET CONNECTION NOT AVAILABLE. Now you are viewing the news in Offline Mode.");
            builder1.setCancelable(true);

            AlertDialog alert1 = builder1.create();
            alert1.show();
    }
    }
    }
类anyClassName扩展异步任务{
/*使用工作线程*/
受保护字符串doInBackground(无效…参数){
if(this.isOnline()){
新建GetJson().execute();
}else{return null}
//…代码的其余部分
返回“”
}
/*使用主线程*/
受保护的void onPostExecute(字符串结果){
如果(结果==null){
AlertDialog.Builder builder1=新建AlertDialog.Builder(homeFragment);
builder1.setMessage(“INTERNET连接不可用。现在您正在以脱机模式查看新闻。”);
builder1.setCancelable(true);
AlertDialog alert1=builder1.create();
alert1.show();
}
}
}
类myAsync扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//TODO自动生成的方法存根
如果(你的工作单){
返回新字符串(“完成”);
}否则{
返回新字符串(“错误消息”);
}
//但是,如果您想检查并更新用户的错误并继续尝试,那么就这样做
//仅使用这两个示例中的一个
如果(你的工作单){
//肯定是完成了,但嘿,这取决于你
publishProgress(新字符串(“完成”));
}否则{
//这是当你有错误和wona让用户知道,并不断尝试击中它的权利
publishProgress(新字符串(“错误消息”));
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
//你的任务完成了
//根据您的选择,您可以将myAlertDialogToShow(上下文、字符串消息)放入
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
}
@凌驾
受保护的void onProgressUpdate(字符串…值){
//TODO自动生成的方法存根
super.onProgressUpdate(值);
//在这里,您的任务正在运行,但您只是通知用户,在ui上运行。。
//根据您的选择,您可以将myAlertDialogToShow(上下文、字符串消息)放入
}
}
void myAlertDialogToShow(上下文、字符串消息){
AlertDialog.Builder builder1=新建AlertDialog.Builder(上下文);
builder1.setMessage(消息);
builder1.setCancelable(true);
AlertDialog alert1=builder1.create();
alert1.show();
}

ayt..检查愚蠢的错误和拼写。。我累了

为什么要在
doInBackground()中设置它
这是一个后台线程。无法在那里执行UI操作。请在onprogressupdate或onpostexecute中执行。。将其覆盖并放置在调用Asyanktask的位置,而不是检查网络是否在此处,并且不在此处。您不能在
doInBackground()
中使用AlertDialog。只需返回结果并根据结果将代码放入
onPost
。处理这种情况的最佳方法是:在调用AsyncTask之前检查Internet连接
 class myAsync extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub          
        if(yourWorkIsDone){
            return new String("done");
        }else{
            return new String("Error message");
        }

        //BUT, if you'd like to check and update user on the error and keep trying then this way
        // use just one of these two examples
        if(yourWorkIsDone){
            // definately is done but hey it depends on you
            publishProgress(new String("done")); 
        }else{
            // this is when you have error and wona let the user know about and keep trying to hit it right
            publishProgress(new String("error message")); 
        }
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        // here your task is done
        // you can put myAlertDialogToShow(Context context,String message) depending on ur choice
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(String... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        // here your task is running but you are just informing user, runs on the ui..
        // you can put myAlertDialogToShow(Context context,String message) depending on ur choice
    }

}

void myAlertDialogToShow(Context context,String message){
    AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
    builder1.setMessage(message);
    builder1.setCancelable(true);
    AlertDialog alert1 = builder1.create();
    alert1.show();

}