title: Debugging Java.Lang.RuntimeException on Xamarin
body: <p>My published game is unconsistently crashing due to this java.exception since late February.
  After about 10 minutes (in my experience it really veries btw 1 min to 30 even 60 mins or never even happens) the game crashes. 
  I've been unable to reproduce the crash with a debugger connected after even 2 hours of gameplay.</p>
  
  <p>I'm using Hockeyapp to report the app crashes but the stacktrace is absolutely empty : </p>
  
  <pre><code>Package: hb.doom_and_destiny_advanced
  Version: 1516
  Android: 5.1.1
  Manufacturer: HUAWEI
  Model: HUAWEI SCL-L01
  Date: 24/06/2016 22:25:51
  
  Java.Lang.RuntimeException: Only the original thread that created a view hierarchy can touch its views.
  </code></pre>
  
  <p>There seems to be no significant reason of failure and the game is not using the UI system if not under the hood with the Monogame Framework.
  After months of testings i really can't help but ask some help to debug this error that is seriously driving me mad.</p>
  
  <p>At the moment the master problem is the unability to reproduce the crash in a consistent and viable time for testing:
  it can occour in 1 minutes or in more than 60 minutes, and it does seem somehow subject to user interaction (i've never been able to see the error while running benchmarks for instance).</p>
  
  <p>Checking the android logs doesn't help either as they contain extremely confusing informations from many of the android systems, but, as far as mono is concerned, it's not verbose at all.</p>
  
  <pre><code>06-20 19:04:16.917: W/ls(27999): type=1400 audit(0.0:3522): avc: denied { getattr } for path="/init" dev="rootfs" ino=5443 scontext=u:r:shell:s0 tcontext=u:object_r:init_exec:s0 tclass=file permissive=0
  06-20 19:04:16.917: W/ls(27999): type=1400 audit(0.0:3523): avc: denied { getattr } for path="/persist" dev="mmcblk0p16" ino=2 scontext=u:r:shell:s0 tcontext=u:object_r:persist_file:s0 tclass=dir permissive=0
  06-20 19:04:18.068: D/ConnectivityService(772): notifyType CAP_CHANGED for NetworkAgentInfo [WIFI () - 101]
  06-20 19:04:19.215: W/SoundPool(12501):   sample 1 not READY
  06-20 19:04:19.254: I/OMXClient(12501): Using client-side OMX mux.
  06-20 19:04:19.258: E/OMXMaster(12501): A component of name 'OMX.qcom.audio.decoder.aac' already exists, ignoring this one.
  06-20 19:04:19.418: W/AudioTrack(12501): AUDIO_OUTPUT_FLAG_FAST denied by client; transfer 4, track 44100 Hz, output 48000 Hz
  06-20 19:04:19.419: D/audio_hw_primary(198): out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=2
  06-20 19:04:19.430: D/audio_hw_primary(198): select_devices: out_snd_device(2: speaker) in_snd_device(0: none)
  06-20 19:04:19.430: D/msm8974_platform(198): platform_send_audio_calibration: sending audio calibration for snd_device(2) acdb_id(15)
  06-20 19:04:19.430: D/audio_hw_primary(198): enable_audio_route: apply and update mixer path: low-latency-playback
  06-20 19:04:19.608: I/art(12501): Starting a blocking GC Explicit
  06-20 19:04:19.624: I/art(12501): Explicit concurrent mark sweep GC freed 3391(130KB) AllocSpace objects, 3(604KB) LOS objects, 25% free, 46MB/62MB, paused 265us total 15.711ms
  06-20 19:04:21.082: D/ConnectivityService(772): notifyType CAP_CHANGED for NetworkAgentInfo [WIFI () - 101]
  06-20 19:04:21.941: D/audio_hw_primary(198): out_set_parameters: enter: usecase(0: deep-buffer-playback) kvpairs: routing=2
  06-20 19:04:21.927: W/ls(28029): type=1400 audit(0.0:3524): avc: denied { getattr } for path="/init" dev="rootfs" ino=5443 scontext=u:r:shell:s0 tcontext=u:object_r:init_exec:s0 tclass=file permissive=0
  06-20 19:04:21.927: W/ls(28029): type=1400 audit(0.0:3525): avc: denied { getattr } for path="/persist" dev="mmcblk0p16" ino=2 scontext=u:r:shell:s0 tcontext=u:object_r:persist_file:s0 tclass=dir permissive=0
  06-20 19:04:22.278: W/SoundPool(12501):   sample 2 not READY
  06-20 19:04:22.292: I/OMXClient(12501): Using client-side OMX mux.
  06-20 19:04:22.296: E/OMXMaster(12501): A component of name 'OMX.qcom.audio.decoder.aac' already exists, ignoring this one.
  06-20 19:04:22.479: W/AudioTrack(12501): AUDIO_OUTPUT_FLAG_FAST denied by client; transfer 4, track 44100 Hz, output 48000 Hz
  06-20 19:04:22.490: D/audio_hw_primary(198): out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=2
  06-20 19:04:22.527: D/AudioFlinger(198): mixer(0xb41c0000) throttle end: throttle time(4)
  06-20 19:04:22.941: D/audio_hw_primary(198): out_set_parameters: enter: usecase(0: deep-buffer-playback) kvpairs: routing=2
  06-20 19:04:25.784: D/audio_hw_primary(198): disable_audio_route: reset and update mixer path: low-latency-playback
  06-20 19:04:26.927: W/ls(28070): type=1400 audit(0.0:3526): avc: denied { getattr } for path="/init" dev="rootfs" ino=5443 scontext=u:r:shell:s0 tcontext=u:object_r:init_exec:s0 tclass=file permissive=0
  06-20 19:04:26.927: W/ls(28070): type=1400 audit(0.0:3527): avc: denied { getattr } for path="/persist" dev="mmcblk0p16" ino=2 scontext=u:r:shell:s0 tcontext=u:object_r:persist_file:s0 tclass=dir permissive=0
  06-20 19:04:27.114: D/ConnectivityService(772): notifyType CAP_CHANGED for NetworkAgentInfo [WIFI () - 101]
  06-20 19:04:28.947: I/art(12501): Starting a blocking GC Explicit
  06-20 19:04:28.960: I/art(12501): Explicit concurrent mark sweep GC freed 2256(88KB) AllocSpace objects, 0(0B) LOS objects, 25% free, 46MB/62MB, paused 285us total 12.462ms
  06-20 19:04:28.984: I/mono-stdout(12501): Writing unhandled exception to: /data/user/0/hb.doom_and_destiny_advanced/files/77b60833-c767-4692-9068-4b829619d394.stacktrace
  </code></pre>
  
  <p><strong>The game does make intensive usage of web calls so i was thinking that maybe android was trying to show a popup if there was inconsistent connection. Since the popup might be called from within a secondary task/thread, it might well crash because of a missing "RunOnUiThread" in the mono implementation by Xamarin.</strong></p>
  
  <p>If this was the case i could check it by publishing with an older version of Xamarin Android, but the Gen 2016 versions are not available anymore for download.</p>
  
  <p>After updating the Exception logging software to latest version the error log seems a little more informative, i still have no idea which thread is crashing but i'm definitely getting some more hits in the searches.</p>
  
  <p>Xamarin caused by: android.runtime.JavaProxyThrowable: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> Java.Lang.RuntimeException: Only the original thread that created a view hierarchy can touch its views.
    --- End of inner exception stack trace ---
  ---> (Inner Exception #0) Java.Lang.RuntimeException: Only the original thread that created a view hierarchy can touch its views.&lt;---</p>
  
  <pre><code>at dalvik.system.NativeStart.run(Native Method)
  </code></pre>
  
  <p>Seems like there is a combination of 2 issues :</p>
  
  <p>1) a task is crashing, likely because of the </p>
  
  <pre><code>(Inner Exception #0) Java.Lang.RuntimeException: Only the original thread that created a view hierarchy can touch its views.&lt;---
  </code></pre>
  
  <p>2) once the gc collects the task the exception gets re-thrown and kills the app with an outer</p>
  
  <pre><code>System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property.
  </code></pre>