问题描述
我试图用一个AlertDialog中Android的检查互联网连接之后,将通知用户他们在离线模式下乳宁。
I am trying to use a AlertDialog in Android which will notify the users that they are runing in offline mode after checking the internet connection.
我用下面的codeS:
I have used the following codes:
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;
}
但我正在逐渐的加入codeS为AlertDialog错误。该应用程序没有codeS为AlertDialog工作正常。
有什么可以与此code中的错误,我怎么能改正它工作得很好??
But I am getting the error on adding the codes for AlertDialog. The app works fine without the codes for AlertDialog.What can be the mistakes with this code and how can i correct it to work well??
推荐答案
doInBackground()在一个单独的线程,比主线程其他运行。您只能在主线程中使用的UI元素。尝试使用runOnUiThread()内doInBackground方法()来显示该对话框。
doInBackground() runs on a separate thread, other than the main thread. You can use UI elements only on main thread. Try using runOnUiThread() method inside doInBackground() to show the dialog.
这篇关于在DoInBackground无法显示AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!