7

I'm having a strange problem which is present only on a specific device (or at least is not present in the ~10 other devices I tested my app on).

Sometimes, when the app should show a progress dialog followed by a Toast containing a message, it crashes instead; looking on Play Console, I see this stack trace:

java.lang.RuntimeException: 
  at android.view.InputChannel.nativeReadFromParcel (Native Method)
  at android.view.InputChannel.readFromParcel (InputChannel.java:148)
  at android.view.IWindowSession$Stub$Proxy.addToDisplay (IWindowSession.java:796)
  at android.view.ViewRootImpl.setView (ViewRootImpl.java:750)
  at android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:298)
  at android.view.WindowManagerImpl.addView (WindowManagerImpl.java:91)
  at android.widget.Toast$TN.handleShow (Toast.java:732)
  at android.widget.Toast$TN$1.run (Toast.java:631)
  at android.os.Handler.handleCallback (Handler.java:739)
  at android.os.Handler.dispatchMessage (Handler.java:95)
  at android.os.Looper.loop (Looper.java:145)
  at android.app.ActivityThread.main (ActivityThread.java:6939)
  at java.lang.reflect.Method.invoke (Native Method)
  at java.lang.reflect.Method.invoke (Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1404)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1199)

The progress bar and the Toast should be shown about once every 5 seconds.

As you can see, everything happens inside Android itself (there is no reference to my actual code), but I believe the following are the involved pieces of code.

This is where the progress bar is shown:

public abstract class FragmentNetworkingBenderAsyncTask<I, O> extends BenderAsyncTask<I, O> {

    @SuppressLint("StaticFieldLeak")
    private final Fragment fragment;
    private AlertDialog waitDialog;

    public FragmentNetworkingBenderAsyncTask(final Fragment fragment) {
        this.fragment = fragment;
    }

    @Override
    protected final void onPreExecute() {
        super.onPreExecute();
        if (fragment.isAdded()) {
            waitDialog = new SpotsDialog.Builder().setContext(fragment.getActivity()).build();           waitDialog.setMessage(MainActivity.commonContext.getString(R.string.Wait));
            waitDialog.show();
            innerOnPreExecute(); //abstract method
        } else {
            this.cancel(true);
        }
    }

    [...]

    @Override
    protected final void onPostExecute(final BenderAsyncTaskResult<O> result) {
        super.onPostExecute(result);
        if (fragment.isAdded()) {
            if (result.isSuccess()) {
                innerOnSuccessfulPostExecute(result); //abstract
            } else {
                innerOnUnsuccessfulPostExecute(result); //abstract
            }
        }
        waitDialog.hide();
    }
}

While this is where the Toast is shown (the class inherits from the one in the previous snippet):

@SuppressLint("StaticFieldLeak")
private class ServerOrdersUploader extends FragmentNetworkingBenderAsyncTask<Order, Empty> {
    ServerOrdersUploader(Fragment fragment) {
        super(fragment);
    }

    [...]

    @Override
    protected void innerOnSuccessfulPostExecute(BenderAsyncTaskResult<Empty> result) {
        Toast.makeText(MainActivity.commonContext, MainActivity.commonContext.getString(R.string.UpdateSuccess), Toast.LENGTH_SHORT).show();
        new ServerOrdersDownloader(TableFragment.this).execute(tableNumber); //ServerOrdersDownloader has a similar behaviour
    }

    [...]
}
    

(full code is available here)

As I said, the problem seems to happen only on a specific device (a Samsung Galaxy Tab A6 with Android 5.1).

What could it be?


Edit 31/07/2020: add code snippets

gscaparrotti
  • 663
  • 5
  • 21
  • Sometimes there are bugs in the Android platform itself, particularly on versions of the OS that have been modified by vendors (and Samsung definitely qualifies here). Perhaps you can use something other than `Toast.makeText()` for these devices. – Ben P. Jul 24 '20 at 18:53
  • You said 'Sometimes, when the app ...', if it shows the progress bar even once correctly then there might be something wrong with the way you repeat the process (using timer or handler) and interacting with the device's resources or maybe wrong usage of the APIs. Either way it may help if you could put logs in some sections of your code to see which ones get executed and at which point the error occurs. – navid Jul 29 '20 at 20:30
  • There is a Timer which makes a thread run every few seconds. There was no problem when there was no progress bar (only the Toast was there), and it works fine on other devices. [Here](https://github.com/gscaparrotti/BenderMobile/blob/develop/app/src/main/java/com/github/gscaparrotti/bendermobile/utilities/FragmentNetworkingBenderAsyncTask.java) is where the progress bar is shown (lines 25-29), while [here](https://github.com/gscaparrotti/BenderMobile/blob/develop/app/src/main/java/com/github/gscaparrotti/bendermobile/fragments/TableFragment.java) is where the toast is shown (line 326). – gscaparrotti Jul 30 '20 at 07:10
  • If You run thread maybe this is solution: https://stackoverflow.com/questions/17410734/toast-is-crashing-application-even-inside-thread?noredirect=1&lq=1 – Wrobel Jul 30 '20 at 10:02
  • If that was the problem it would be present on every device and it would happen every time. Besides, I already do that. – gscaparrotti Jul 30 '20 at 10:57
  • Can you share your code, please ? – Gk Mohammad Emon Jul 31 '20 at 02:25
  • Have you found a solution yet? – Lazaros Papadopoulos Aug 14 '21 at 20:06
  • I think it is a bug in the device itself rather than my app. Anyway, I decided to display the progress in a different way (updating a label inside the app's title bar), so that it works correctly on (hopefully) every device. – gscaparrotti Aug 16 '21 at 06:43

1 Answers1

1

It might be a bug with the OS itself as @Ben P. said, I feel the cause of the issue is when both progress bar and toast are sort of overlapped and the OS is buggy to handle it. Try Executing one after the other one is finished