private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
AlertDialog.Builder bu=new AlertDialog.Builder(getApplicationContext());
bu.setTitle("Sorry");
bu.setMessage("Your time is up. Score was:"+(score*5));
bu.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
AlertDialog a =bu.create();
a.show();
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
此活动需要使用Theme.AppCompat主题(或后代)。 这是我在onPostExecute()方法上遇到的异常