From b997a2581b9a1254584f4dbfdcfe00248bf09a30 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 9 Jul 2018 17:29:24 +0200 Subject: [PATCH] BaseFragment: in the event no view is available, use toast Signed-off-by: Jason A. Donenfeld --- .../wireguard/android/fragment/BaseFragment.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java b/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java index 510b1cae..e1d4c87b 100644 --- a/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java @@ -15,6 +15,7 @@ import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; import android.util.Log; import android.view.View; +import android.widget.Toast; import com.wireguard.android.Application; import com.wireguard.android.R; @@ -109,16 +110,14 @@ public abstract class BaseFragment extends Fragment implements OnSelectedTunnelC tunnel.setState(State.of(checked)).whenComplete((state, throwable) -> { if (throwable == null) return; - final View view = getView(); - if (view == null) { - Log.e(TAG, "setTunnelStateWithPermissionsResult() with no view"); - return; - } - final Context context = view.getContext(); final String error = ExceptionLoggers.unwrapMessage(throwable); final int messageResId = checked ? R.string.error_up : R.string.error_down; - final String message = context.getString(messageResId, error); - Snackbar.make(view, message, Snackbar.LENGTH_LONG).show(); + final String message = getContext().getString(messageResId, error); + final View view = getView(); + if (view != null) + Snackbar.make(view, message, Snackbar.LENGTH_LONG).show(); + else + Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show(); Log.e(TAG, message, throwable); }); }