2

I'm was testing ClipboardManager to copy a String to the device's clipboard when clicking a button. I started spam clicking the button to test it and it ended up crashing my emulator.

//In my activity

@OnClick(R.id.btn_copy_address)
    void onCopyAddressClick() {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("Text label", "123 Sesame Street");
        clipboard.setPrimaryClip(clip);
}

enter image description here

The error being thrown in logs:

enter image description here

Second attempt: This was caused by having a Toast after the ClipboardCopy. No longer threw this error once I removed the Toast

``` java.lang.RuntimeException: Adding window failed at android.view.ViewRootImpl.setView(ViewRootImpl.java:738) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) at android.widget.Toast$TN.handleShow(Toast.java:496) at android.widget.Toast$TN$1.handleMessage(Toast.java:400) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: android.os.DeadObjectException at android.os.BinderProxy.transactNative(Native Method) at android.os.BinderProxy.transact(Binder.java:764) at android.view.IWindowSession$Stub$Proxy.addToDisplay(IWindowSession.java:791) at android.view.ViewRootImpl.setView(ViewRootImpl.java:726) ```

Third attempt:

enter image description here

The fourth attempt returns the same error log as the third.

Does anyone know what the cause of the crash is? Or how I can fix it?

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
  • 1
    `Or how I can fix it?` well, preventing the user from spamming it would probably be a good start :) disable after first tap, perhaps enable after a few seconds again, although this is entirely irrelevant as to why it happens, very interesting, wouldn't be surprised if this is just an emulator issue. shouldn't `(ClipboardManager) getSystemService(...` be wrapped in a try catch though ? – a_local_nobody Jan 25 '22 at 08:31
  • Oh okay I'll probably do that. It's not throwing any lint errors if I don't wrap it with a trycatch. But I just tried it with one just in case, and it's still crashing. – DIRTY DAVE Jan 25 '22 at 08:35
  • yeah this doesn't seem to be an app-level crash, not if the entire emulator is restarting, so i doubt anything you try to do from the app side will fix this, i just suggested the try catch because usually when you access system services you need a try catch for actual devices – a_local_nobody Jan 25 '22 at 08:38
  • I didn't know that thanks for the tip – DIRTY DAVE Jan 25 '22 at 08:40
  • well, that's from my understanding at least, i believe sometimes it's possible that a device might not have a specific system service so your attempt at retrieving and casting it _could_ fail, but don't take this as law. regardless, your question is now irrelevant if you caused this with the toast, not sure if you want to delete this or not – a_local_nobody Jan 25 '22 at 08:42
  • 1
    It's still crashing. It just only no longer threw the second error because I removed the toast. – DIRTY DAVE Jan 25 '22 at 08:43

0 Answers0