From 94d72d768fe67e0e9174601e303889c84abc6132 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 27 Sep 2017 12:50:35 +0900 Subject: [PATCH] Attempt to fix NPE when calling SyncthingService.getApi().isConfigLoaded() --- .../activities/SettingsActivity.java | 12 ++++++++---- .../syncthingandroid/service/SyncthingService.java | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java index 2b235c59..e2331174 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -175,10 +175,14 @@ public class SettingsActivity extends SyncthingActivity { mSyncthingService = ((SyncthingActivity) getActivity()).getService(); mSyncthingService.registerOnApiChangeListener(this); - if (mSyncthingService.getApi().isConfigLoaded()) { - mGui = mSyncthingService.getApi().getGui(); - mOptions = mSyncthingService.getApi().getOptions(); - } + // Use callback to make sure getApi() doesn't return null. + mSyncthingService.registerOnWebGuiAvailableListener(() -> { + if (mSyncthingService.getApi().isConfigLoaded()) { + mGui = mSyncthingService.getApi().getGui(); + mOptions = mSyncthingService.getApi().getOptions(); + } + + }); } @Override diff --git a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java index 52434221..b619989a 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java @@ -15,6 +15,7 @@ import android.os.Environment; import android.os.IBinder; import android.os.PowerManager; import android.preference.PreferenceManager; +import android.support.annotation.Nullable; import android.support.v4.app.NotificationCompat; import android.util.Log; import android.util.Pair; @@ -457,7 +458,7 @@ public class SyncthingService extends Service implements return !new File(getFilesDir(), PUBLIC_KEY_FILE).exists(); } - public RestApi getApi() { + public @Nullable RestApi getApi() { return mApi; }