我正在尝试在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的代码。 这段代码的错误是什么?我如何纠正它才能正常工作?
答案 0 :(得分:1)
doInBackground()在除主线程之外的单独线程上运行。您只能在主线程上使用UI元素。尝试在doInBackground()中使用runOnUiThread()方法来显示对话框。
答案 1 :(得分:0)
这里AlertDialog不起作用,你可以使用 System.out.println(“some text”); 如果您仅使用警报进行测试。 否则你必须设置一个参数以及你想要放入alertbox的文本,只需在主线程中使用它。
如果您有任何疑惑,请告诉我。
答案 2 :(得分:0)
在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();
}
}
}
答案 3 :(得分:0)
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();
}
ayt..check愚蠢的错误和拼写..我很累