捕获库在另一个线程中抛出的异常(android)

时间:2018-12-18 21:41:24

标签: java android exception

我使用的库有时(大约100次中的1次)抛出ConcurrentModificationException,导致应用程序崩溃。

stacktrace没有提到我的任何代码,而是库的代码。 我试图在调用库的地方捕获异常,但是它不起作用。

我猜想异常是在库中的新线程中引发的。

我如何捕获这样的异常?

库: 旧的Philips Hue Java库。 (我将很快切换到新的。)

例外:

E/AndroidRuntime: FATAL EXCEPTION: Thread-21319
    Process: my.package, PID: 21561
    Theme: themes:{...}
    java.util.ConcurrentModificationException
        at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
        at com.philips.lighting.hue.sdk.notification.impl.PHNotificationManagerImpl$3.onReceived(PHNotificationManagerImpl.java:180)
        at com.philips.lighting.hue.sdk.notification.impl.PHHueResultReceiver.execute(PHHueResultReceiver.java:24)
        at com.philips.lighting.hue.sdk.notification.impl.PHNotificationManagerImpl.notifyBridgeSearchResult(PHNotificationManagerImpl.java:188)
        at com.philips.lighting.hue.sdk.upnp.PHBridgeSearchManagerImpl$1.run(PHBridgeSearchManagerImpl.java:198)

1 个答案:

答案 0 :(得分:0)

我猜想您在遍历数组值时会修改它。

  

Java集合类是快速失败的,这意味着如果集合   将在某些线程遍历时使用更改   迭代器,iterator.next()将抛出   ConcurrentModificationException。

这里是如何避免它: How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList