From 868d86eee0de1f512d211601a5b253fa784bb915 Mon Sep 17 00:00:00 2001 From: Marc Nause Date: Wed, 22 Sep 2021 20:56:57 +0200 Subject: [PATCH] Fixes #2211 fix rare crash --- .../java/io/pslab/fragment/HomeFragment.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/pslab/fragment/HomeFragment.java b/app/src/main/java/io/pslab/fragment/HomeFragment.java index 911200c39..225f591f1 100644 --- a/app/src/main/java/io/pslab/fragment/HomeFragment.java +++ b/app/src/main/java/io/pslab/fragment/HomeFragment.java @@ -1,5 +1,7 @@ package io.pslab.fragment; +import static io.pslab.others.ScienceLabCommon.scienceLab; + import android.graphics.Bitmap; import android.graphics.Paint; import android.os.Bundle; @@ -28,8 +30,6 @@ import io.pslab.others.InitializationVariable; import io.pslab.others.ScienceLabCommon; -import static io.pslab.others.ScienceLabCommon.scienceLab; - /** * Created by viveksb007 on 15/3/17. */ @@ -107,9 +107,22 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c imgViewDeviceStatus.setImageResource(R.drawable.icons_usb_disconnected_100); tvDeviceStatus.setText(getString(R.string.device_not_found)); } + + /* + * The null-checks in the OnClickListener may seem unnecessary, but even though the + * respective variables are initialized before the setter is called, they may contain null + * in later phases of the lifecycle of this Fragment and cause NullPointerExceptions if not + * checked before access. + * + * See: https://github.com/fossasia/pslab-android/issues/2211 + */ deviceDescription.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + if (webView == null) { + return; + } + webView.loadUrl("https://pslab.io"); webView.getSettings().setDomStorageEnabled(true); webView.getSettings().setJavaScriptEnabled(true); @@ -117,13 +130,19 @@ public void onClick(View view) { webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { - wvProgressBar.setIndeterminate(true); - wvProgressBar.setVisibility(View.VISIBLE); + if (wvProgressBar != null) { + wvProgressBar.setIndeterminate(true); + wvProgressBar.setVisibility(View.VISIBLE); + } } public void onPageFinished(WebView view, String url) { - wvProgressBar.setVisibility(View.GONE); - webView.setVisibility(View.VISIBLE); + if (wvProgressBar != null) { + wvProgressBar.setVisibility(View.GONE); + } + if (webView != null) { + webView.setVisibility(View.VISIBLE); + } } }); isWebViewShowing = true;