Так что у меня проблемы с моим AsyncTask
. Мне нужно postExecute
, чтобы отобразить диалоговое окно с предупреждением, если определенная ошибка может быть удалена в doInBackground
. Проблема в том, что postExecute
никогда не вызывается. Я попытался добавить @Override
, но Android Studio говорит, что он не переопределяет метод в своем суперклассе. Я также попытался изменить тип возврата. Я осмотрел этот сайт и не нашел ответа. Заранее спасибо.onPostExecute не называется
AsyncTask Класс
public class AsyncTaskActivity extends AsyncTask<Void, Void, Void> {
String exception;
@Override
protected void onPreExecute() {
}
protected void onPostExecute() {
if (exception.contains("java.net.UnknownHostException")) {
MainActivity.showDialog();
Log.i("Error Message", "ERROR MESSAGE SHOWN");
}
}
@Override
protected Void doInBackground(Void... params) {
try {
Log.i("AsyncTask", "Loading...");
// Make a URL to the web page. This takes the string representation of the URL
// and changes it into a URL object
URL url = new URL("http://api.wunderground.com/api/0c0fcc3bf62ab910/conditions/q/IN/Fort_Wayne.json");
// Get the input stream through URL Connection
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
// read each line and write to text file
while ((line = br.readLine()) != null) {
Log.i("AsyncTask", line);
TextEditor.file = new File(MainActivity.path, "siteInfo.txt");
TextEditor.writeString(line);
}
TextEditor.saveAndClose();
} catch (Exception e) {
e.printStackTrace();
exception = e.toString();
}
Log.i("AsyncTask", "DONE");
return null;
}
}
метод ShowDialog
public static void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.context);
builder.setView(R.layout.dialog_layout);
builder.setPositiveButton(
R.string.dialog_close,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
System.exit(1);
}
});
builder.show();
}
Посмотрите на сигнатуру метода для onPostExecute() из документов вам не хватает параметра. –
где @Override в onPostExecute? –