From 718f42fc9d3342e49949b52ad81055a562b823e1 Mon Sep 17 00:00:00 2001 From: dimitris Date: Wed, 20 May 2026 13:26:31 +0200 Subject: [PATCH] MainActivity: catch ActivityNotFoundException in WebView URL handoff MyWebViewClient.shouldOverrideUrlLoading forwards every URL the embedded HTML page navigates to with a plain startActivity(Intent. ACTION_VIEW). If the device has no installed app that handles the URI (a tablet without a browser, a stripped GSI build, a tel: link on a non-phone form factor, ...), startActivity throws ActivityNotFoundException and the Activity is torn down mid-game. Wrap the launch in try / catch and surface a Toast so a missing handler scopes the failure to that single click instead of crashing the whole Activity. --- .../java/org/olgsoft/apipepanic/MainActivity.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/olgsoft/apipepanic/MainActivity.java b/app/src/main/java/org/olgsoft/apipepanic/MainActivity.java index 9a80bb5..24b9200 100644 --- a/app/src/main/java/org/olgsoft/apipepanic/MainActivity.java +++ b/app/src/main/java/org/olgsoft/apipepanic/MainActivity.java @@ -3,6 +3,7 @@ import android.annotation.SuppressLint; import android.app.Activity; +import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.res.Configuration; import android.graphics.Bitmap; @@ -26,6 +27,7 @@ import android.widget.CheckBox; import android.widget.SeekBar; import android.widget.TextView; +import android.widget.Toast; public class MainActivity extends Activity { @@ -39,7 +41,14 @@ private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); - startActivity(intent); + try { + startActivity(intent); + } catch (ActivityNotFoundException e) { + Log.w("PipePanic", "No app to open " + url, e); + Toast.makeText(MainActivity.this, + "No app to open this link", + Toast.LENGTH_LONG).show(); + } return true; }