Charts: Avoid crash if database fetch fails

This commit is contained in:
José Rebelo
2025-06-26 18:08:54 +01:00
parent a291eb1205
commit 453eaee4c1
2 changed files with 16 additions and 5 deletions
@@ -368,12 +368,18 @@ public abstract class AbstractChartFragment<D extends ChartsData> extends Abstra
protected void onPostExecute(final Object o) {
super.onPostExecute(o);
final FragmentActivity activity = getActivity();
if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
updateChartsnUIThread(chartsData);
renderCharts();
} else {
if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
LOG.info("Not rendering charts because activity is not available anymore");
return;
}
if (getTaskError() != null) {
// Async task failed - we will have no data, so avoid NPE crashes
// a log + toast were already displayed by the DBAccess class
return;
}
updateChartsnUIThread(chartsData);
renderCharts();
}
}
@@ -20,6 +20,8 @@ import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import androidx.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -63,7 +65,10 @@ public abstract class DBAccess extends AsyncTask {
}
}
@Nullable
public Exception getTaskError() {
return mError;
}
protected void displayError(Throwable error) {
GB.toast(getContext(), getContext().getString(R.string.dbaccess_error_executing, error.getMessage()), Toast.LENGTH_LONG, GB.ERROR, error);