0

I am trying to show a Toast when my API-Request doesn't work due to No-Internet-Connection. To start of easy, I just tried to show a Toast as soon as the try-block fails and then I want to show the Toast in the catch-block. I tried the approach from developer.android.com but I can't even start the Application.

My code in the catch-block is this:

} catch {
Context context = getApplicationContext();
Toast.makeText(context, "No Internet Connection.", Toast.LENGTH_SHORT).show();
}

I seem to have problems with the getApplicationContext() Method, because this is highlighted red. It seems to be a problem because I am using this Inside a Fragment. I read something with using Activity activity = getActivity() but when trying to insert this activity as the context in the makeText() Method, I get this Error Message:

E/AndroidRuntime: FATAL EXCEPTION: Thread-4
    Process: com.example.dhapp, PID: 16158
    java.lang.NullPointerException: Can't toast on a thread that has not called Looper.prepare()
        at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:157)
        at android.widget.Toast.getLooper(Toast.java:287)
        at android.widget.Toast.<init>(Toast.java:256)
        at android.widget.Toast.makeText(Toast.java:891)
        at android.widget.Toast.makeText(Toast.java:879)
        at com.example.dhapp.SearchFragment$apiThread.run(SearchFragment.java:112)

Any tips on how I get this to work? Thanks a lot in Advance!

Marc
  • 13
  • 5
  • The code you posted has a typo: it should be `getApplicationContext()` but you posted `getApplicationcontext()`. Perhaps that's why the method is highlighted red. As for the exception, that may be because you are executing the `catch` block in a worker thread. You'd have to post more code and an explanation of what you're doing for us to help you with that. – Ted Hopp Apr 02 '21 at 15:15
  • duplicate of https://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare – CSmith Apr 02 '21 at 15:29
  • check here [Show toast within try-catch block](https://stackoverflow.com/questions/20177023/show-toast-within-try-catch-block) – Bristol Apr 02 '21 at 15:34
  • check @srinij 's answer – SABANTO Apr 02 '21 at 16:13

3 Answers3

2

It sounds like you are NOT calling the Toast.makeText() from within the main thread. The thread that you call from needs access to the UI essentially for the Toast to display.

This SO thread might give you some pointers.

srinij
  • 471
  • 6
  • 10
1

You CANNOT show a Toast on non-main thread. You need to call Toast.makeText() (and most other functions dealing with the UI) from within the main thread. You can find some solutions on Can't toast on a thread that has not called Looper.prepare(), How do you display a Toast from a background thread on Android?

The problem with the getApplicationContext() is that you are inside a fragment you should get The activity context this way

Toast.makeText(getContext(), "No Internet Connection.", Toast.LENGTH_SHORT).show();

The getContext() method it's implemented on Fragments.java so you dont need to call getApplicationContext() because it is only available from activities not from fragments.

   /**
     * Return the {@link Context} this fragment is currently associated with.
     *
     * @see #requireContext()
     */
    @Nullable
    public Context getContext() {
        return mHost == null ? null : mHost.getContext();
    }
0

Instead of using getApplicationContext() you could try requireActivity()

marcrobito
  • 23
  • 3