问题
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.
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;
}
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??
回答1:
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.
回答2:
here AlertDialog will not work, you can use System.out.println("some text"); if you are using alert just for testing purpose. otherwise you have to set into a parameter and what ever text you want to put in alertbox and just use it in main thread.
if you have any confusion please let me know .
回答3:
In Android you can do UI operations only in main thread. So you can show dialog in onPostExecute method. Example:
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();
}
}
}
回答4:
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 for silly errors and spellings.. im tired
来源:https://stackoverflow.com/questions/27499292/cant-show-alertdialog-in-doinbackground