Skip to content

Commit

Permalink
Avoid NullPointerException if clipboard is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
Neamar committed Dec 16, 2022
1 parent b7a48d6 commit c52ee51
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ public ImportSettingsPreference(Context context, AttributeSet attrs) {
public void onClick(DialogInterface dialog, int which) {
super.onClick(dialog, which);
if (which == DialogInterface.BUTTON_POSITIVE) {
// Apply changes
ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(CLIPBOARD_SERVICE);
String clipboardText = clipboard.getPrimaryClip().getItemAt(0).coerceToText(getContext()).toString();
try {
// Apply changes
ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(CLIPBOARD_SERVICE);
// Can throw NullPointerException if the application doesn't have focus. Display a toast if this happens
String clipboardText = clipboard.getPrimaryClip().getItemAt(0).coerceToText(getContext()).toString();

// Validate JSON
JSONObject o = new JSONObject(clipboardText);
if (o.getInt("__v") > BuildConfig.VERSION_CODE) {
Expand Down Expand Up @@ -87,7 +89,7 @@ public void onClick(DialogInterface dialog, int which) {
}

Toast.makeText(getContext(), "Preferences imported!", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
} catch (JSONException|NullPointerException e) {
e.printStackTrace();
Toast.makeText(getContext(), "Unable to import preferences", Toast.LENGTH_SHORT).show();
}
Expand Down

0 comments on commit c52ee51

Please sign in to comment.