Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid freezes when there is a NullPointerException in OnClickListener.onClick (other tasks cannot start)
    primarykey
    data
    text
    <p>I have a simple setup:</p> <ul> <li><code>CrashHandler</code> - a class which implements <code>Thread.UncaughtExceptionHandler</code>;</li> <li><code>CrashActivity</code> - an activity which can send user reports;</li> <li><code>MainActivity</code> - the main app with which the user should interact. </li> </ul> <p>When there is an uncaught exception within the <code>MainActivity</code> or any of it's threads, the <code>CrashHandler</code> intercepts it and creates a notification with an intent to start the <code>CrashActivity</code>:</p> <pre><code>Intent it = new Intent("CrashReporter" + SystemClock.currentThreadTimeMillis()); it.setClass(context, CrashActivity.class); it.setFlags(it.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); </code></pre> <p>In the mean time Android shows an "Application crashed" message, the user clicks OK, the application is closed, then the user might click the <code>notification</code>. If the <code>notification</code> is clicked, the <code>CrashActivity</code> starts and shows.</p> <p>This code has been working for a long time in many different situations (crash on main thread, crash in a <code>handler</code>, crash on a background <code>thread</code>...). However, I recently discovered that it DOES NOT WORK if the exception is thrown in the <code>OnClickListener.onClick</code> method in a listener attached to a <code>button</code> in the <code>MainActivity</code>. The situation is as follows:</p> <ol> <li>I execute code that deliberately throws a <code>NullPointerException</code>;</li> <li><code>CrashHandler</code> intercepts it and creates a <code>notification</code> (which is shown);</li> <li>Android DOES NOT show any messages (for example, there is no "Application crashed", which should be visible);</li> <li>The <code>MainActivity</code> is frozen;</li> <li>IF the user clicks on the notification to launch the <code>CrashActivity</code>, a black screen is shown and everything freezes (the desired activity is not shown).</li> </ol> <p><code>Logcat</code> shows that there is a timeout on launch, even before <code>OnCreate</code> or any of my code:</p> <pre><code>I/ActivityManager(11826): START u0 {act=CrashHandler1196 flg=0x14000000 cmp=mycompany.myapp/.CrashActivity bnds=[0,102][720,230] (has extras)} from pid -1 W/KeyguardViewMediator(11826): verifyUnlock called when not externally disabled W/ActivityManager(11826): Activity pause timeout for ActivityRecord{41f4d988 u0 mycompany.myapp/.MainActivity} W/ActivityManager(11826): Launch timeout has expired, giving up wake lock! W/ActivityManager(11826): Activity idle timeout for ActivityRecord{4225eeb8 u0 mycompany.myapp/.CrashActivity} </code></pre> <ul> <li>If before clicking the <code>notification</code> I kill the app from <code>ADB</code>, the <code>notification</code> works perfectly.</li> <li><p>If before clicking the <code>notification</code> I make some clicks and gestures on the frozen app, after a few seconds I receive a message about an <code>ANR</code>: </p> <pre><code> E/ActivityManager(11826): ANR in mycompany.myapp (mycompany.myapp/.MainActivity) E/ActivityManager(11826): Reason: keyDispatchingTimedOut E/ActivityManager(11826): Load: 0.63 / 0.57 / 0.49 </code></pre></li> <li><p>If I click "yes, kill it", and then click the <code>notification</code>, it works perfectly.</p></li> <li>If I add <code>System.exit</code>(-1) in the <code>CrashHandler</code> just after creating the notification, the app immediately quits and the notification works perfectly (unfortunately, I can't go with this solution in production). </li> </ul> <p>I have two questions:</p> <ol> <li>Why a <code>NullPointer exception</code> in <code>OnClickListener.onClick</code> doesn't cause the app to crash, instead freezing it together with the OS and preventing other activities from starting?</li> <li>What to do to avoid it in general, or at least, how to make the <code>CrashActivity</code> start in these conditions? </li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload