Well, I managed to "fix" this problem. This is not really a fix, but at least I avoid a crash in my application when this exception is thrown. What I did was to override the default exception thrower and suppress the application crash when it is an uncaught exception. The accident occurred due to a NullPointerException exception, and this always happened on GLThread (must be from a drawing code from Google Maps). So I check if all the preconditions apply, and if so, I send a broadcast with a custom action so that I can listen to this broadcast on the snippet on which I use the Google Map, replacing my SupportMapFragment when I receive it. Since we have an uncaught exception, GLThread is aborted and the drawing in the Google Map Fragment is paused, so we need to replace the Google Maps fragment to make the map work again.
Here is the code I had to add:
In my onCreate app:
final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable ex) { if (ex instanceof NullPointerException && thread.getName().startsWith("GLThread")) { for (StackTraceElement stackTraceElement : ex.getStackTrace()) { if (stackTraceElement.getClassName().contains("maps")) { sendBroadcast(new Intent(ACTION_MAPS_NPE)); return; } } } defaultHandler.uncaughtException(thread, ex); } });
And then on my snippet that uses the Google Maps snippet, I will register this broadcast receiver right before adding SupportMapFragment to the layout on onCreateView (and unregister on onDestroyView):
private class GoogleMapsNPEReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Fragment mapFragment = getChildFragmentManager().findFragmentById(R.id.map_fragment_container); if (mapFragment != null) { getChildFragmentManager().beginTransaction() .replace(R.id.map_fragment_container, SupportMapFragment.newInstance()) .commit(); mMapInitialized = false;
Hope this helps someone with the same issue. If anyone has the actual fix, let me know.
source share