如果您对如何在Android中处理ConcurrentModificationException感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于如何在Android中处理Co
如果您对如何在Android中处理ConcurrentModificationException感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于如何在Android中处理ConcurrentModificationException的详细内容,我们还将为您解答android.intent.action.media_mounted的相关问题,并且为您提供关于android – Drag’n’Drop ConcurentModificationException、android – java.util.ConcurrentModificationException和SharedPreference、android – java.util.ConcurrentModificationException在MapView上、Android – setVisibility导致java.util.ConcurrentModificationException的有价值信息。
本文目录一览:- 如何在Android中处理ConcurrentModificationException(android.intent.action.media_mounted)
- android – Drag’n’Drop ConcurentModificationException
- android – java.util.ConcurrentModificationException和SharedPreference
- android – java.util.ConcurrentModificationException在MapView上
- Android – setVisibility导致java.util.ConcurrentModificationException
如何在Android中处理ConcurrentModificationException(android.intent.action.media_mounted)
首先我尝试通过array_list_name.remove(i)删除它们,但它失败了,有些人被要求使用Iterator.所以我目前的代码如下.
for (Iterator<Collectable> iter = array_list_name.iterator(); iter.hasNext();) { Collectable s = iter.next(); if (s.equals(array_list_name.get(id))){ iter.remove(); return true; } }
我在视图中的onDraw()函数中调用“array_list_name”.我的观点是SurfaceView.任何人都可以建议我如何从ArrayList中删除项目而不会收到此错误.
解决方法
android – Drag’n’Drop ConcurentModificationException
@Override public boolean onDrag(View v,DragEvent event) { switch (event.getAction()) { case DragEvent.ACTION_DRAG_ENTERED: switch (v.getId()) { case R.id.delete_zone: { addToShowCaseZone.setimageDrawable(getResources().getDrawable(R.drawable.showcase_2)); inAddToShowcasesZone = true; break; } case MagazineGridAdapter.ID: { enteredView = v; break; } } return false; case DragEvent.ACTION_DRAG_EXITED: { switch (v.getId()) { case R.id.delete_zone: { addToShowCaseZone.setimageDrawable(getResources().getDrawable(R.drawable.showcase_1)); inAddToShowcasesZone = false; break; } case MagazineGridAdapter.ID: { enteredView = null; break; } } return true; } case DragEvent.ACTION_DRAG_STARTED: return true; case DragEvent.ACTION_DRAG_LOCATION: return false; case DragEvent.ACTION_DROP: { if (inAddToShowcasesZone) { final int position = gridView.getPositionForView(dragView); Magazine magazine = magazineAdapter.getItem(position); try { new Magazine(magazine.getUrl().toString(),magazine.getimage(),magazine.getBackgroundNum(),magazine.getName()); } catch (MalformedURLException e) { e.printstacktrace(); } addToShowCaseZone.setimageDrawable(getResources().getDrawable(R.drawable.showcase_1)); inAddToShowcasesZone = false; magazineAdapter.deleteFromList(position); return false; } else if(enteredView != null && !enteredView.equals(dragView)){ ResourcesForNativeMagazines.swapItems(gridView.getPositionForView(dragView),gridView.getPositionForView(enteredView),tabNumber - 1); magazineAdapter.refreshValues(ResourcesForNativeMagazines.getMagazines(tabNumber - 1)); enteredView = null; return false; } dragView.setVisibility(VISIBLE); return false; } default: dragView.setVisibility(VISIBLE); return true; } }
适配器部件:
public void refreshValues(List<Magazine> magazines){ this.magazines = new ArrayList<>(magazines); notifyDataSetChanged(); } public void deleteFromList(int position){ magazines.remove(position); notifyDataSetChanged(); }
有时这个代码在我删除项目时调用方法refreshValuews和deleteFromList中的错误,这是它的堆栈跟踪:
java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:806) at java.util.HashMap$KeyIterator.next(HashMap.java:833) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1172) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174) at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1174) at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:4911) at android.view.ViewRootImpl.access$700(ViewRootImpl.java:94) at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3188) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method)
我应该怎么解决?有没有其他方法可以通过拖放来修改gridView中的项目?
解决方法
public boolean onDrag(View v,DragEvent event) { switch (event.getAction()) { case DragEvent.ACTION_DRAG_ENDED:{ v.post(new Runnable{ public void run() { //SomeCode; } }); break; } } }
android – java.util.ConcurrentModificationException和SharedPreference
java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:787) at java.util.HashMap$KeyIterator.next(HashMap.java:814) at com.android.internal.util.XmlUtils.writeSetXml(XmlUtils.java:350) at com.android.internal.util.XmlUtils.writeValueXml(XmlUtils.java:688) at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:295) at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:264) at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:230) at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:187) at android.app.SharedPreferencesImpl.writetoFile(SharedPreferencesImpl.java:597) at android.app.SharedPreferencesImpl.access$800(SharedPreferencesImpl.java:51) at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:512) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)
解决方法
如果您使用getAll并修改返回的地图,也会发生这种情况.
见documentation
android – java.util.ConcurrentModificationException在MapView上
我很多天都面临着非常奇怪的问题.我正在尝试频繁更新叠加层.所以有时我在地图上触摸时会得到“java.util.ConcurrentModificationException”,或者当地图试图更新叠加时有时会得到但是我找不到这个错误即将到来的完美线.
02-17 14:56:01.621: W/dalvikvm(3653): threadid=1: thread exiting with uncaught exception (group=0x40015560) 02-17 14:56:01.631: E/AndroidRuntime(3653): FATAL EXCEPTION: main 02-17 14:56:01.631: E/AndroidRuntime(3653): java.util.ConcurrentModificationException 02-17 14:56:01.631: E/AndroidRuntime(3653): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:576) 02-17 14:56:01.631: E/AndroidRuntime(3653): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:41) 02-17 14:56:01.631: E/AndroidRuntime(3653): at com.google.android.maps.MapView.onDraw(MapView.java:530) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.View.draw(View.java:6880) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.View.draw(View.java:6883) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.widget.FrameLayout.draw(FrameLayout.java:357) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.View.draw(View.java:6883) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.widget.FrameLayout.draw(FrameLayout.java:357) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.View.draw(View.java:6883) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.widget.FrameLayout.draw(FrameLayout.java:357) 02-17 14:56:01.631: E/AndroidRuntime(3653): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewRoot.draw(ViewRoot.java:1522) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.os.Handler.dispatchMessage(Handler.java:99) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.os.Looper.loop(Looper.java:130) 02-17 14:56:01.631: E/AndroidRuntime(3653): at android.app.ActivityThread.main(ActivityThread.java:3683) 02-17 14:56:01.631: E/AndroidRuntime(3653): at java.lang.reflect.Method.invokeNative(Native Method) 02-17 14:56:01.631: E/AndroidRuntime(3653): at java.lang.reflect.Method.invoke(Method.java:507) 02-17 14:56:01.631: E/AndroidRuntime(3653): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 02-17 14:56:01.631: E/AndroidRuntime(3653): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 02-17 14:56:01.631: E/AndroidRuntime(3653): at dalvik.system.NativeStart.main(Native Method)
我正在使用Balloon Overlay
我认为它在“mapview.getoverlay()”上抛出错误,因为它在1秒钟内调用了4-5次.
谢谢,
CapDroid
这是代码流..
我的线程代码….
Thread connection = new Thread(){ public void run() { try { while (my condition) { try { //This method should be call every seconds updateMethod(); } catch (Exception e) { e.printstacktrace(); break; } } } catch (Exception e) { } } };
这是我的updateMethod();
在这段代码中,hashmapOverlay是HashMap,如下所示
public static Map<String,Myitemizedoverlay> hashmapOverlay; public void updateMethod() { if(hashmapOverlay.containsKey(id)) { mapview.getoverlays().remove(hashmapOnlineFriendsOverlay.get(id)); } Myitemizedoverlay mMyitemizedoverlay = new Myitemizedoverlay(drawable,mapview); OverlayItem overlayItem = new OverlayItem(gp,title,snippet); mMyitemizedoverlay.addOverlay(overlayItem); hashmapOverlay.put(id,mMyitemizedoverlay); addOverlayMethod(mActivity,mapView,mMyitemizedoverlay); }
这是我的addOverlayMethod
addOverlayMethod(Activity mActivity,final MapView mapView,final Object mObject) { mActivity.runOnUiThread(new Runnable(){ @Override public void run() { try { Myitemizedoverlay overlay = (Myitemizedoverlay) mObject; mapView.getoverlays().add(overlay); } catch (Exception e) {} } }); }
解决方法
步骤如下.
>制作Pendingnotification类(修改细节).
>列出Pendingnotification.
>当任何通知收到dnt直接修改它时,收集所有修改并放入堆栈(Pendingnotification类)即.将其添加到Pendingnotification列表中.
>现在逐个修改对象,并在完成工作时删除相同的对象.
同样的方式跟随直到Pendingnotification List变空.
并且还要注意,任何修改都必须在UI线程中执行,而不是在NonUI线程中执行
Android – setVisibility导致java.util.ConcurrentModificationException
03-28 01:32:05.450: E/AndroidRuntime(20895): FATAL EXCEPTION: main 03-28 01:32:05.450: E/AndroidRuntime(20895): java.util.ConcurrentModificationException 03-28 01:32:05.450: E/AndroidRuntime(20895): at java.util.HashMap$HashIterator.nextEntry(HashMap.java:796) 03-28 01:32:05.450: E/AndroidRuntime(20895): at java.util.HashMap$KeyIterator.next(HashMap.java:823) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:946) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:948) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:948) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:948) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:948) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:948) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewRoot.handleDragEvent(ViewRoot.java:3027) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.view.ViewRoot.handleMessage(ViewRoot.java:2185) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.os.Handler.dispatchMessage(Handler.java:99) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.os.Looper.loop(Looper.java:132) 03-28 01:32:05.450: E/AndroidRuntime(20895): at android.app.ActivityThread.main(ActivityThread.java:4028) 03-28 01:32:05.450: E/AndroidRuntime(20895): at java.lang.reflect.Method.invokeNative(Native Method) 03-28 01:32:05.450: E/AndroidRuntime(20895): at java.lang.reflect.Method.invoke(Method.java:491) 03-28 01:32:05.450: E/AndroidRuntime(20895): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 03-28 01:32:05.450: E/AndroidRuntime(20895): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 03-28 01:32:05.450: E/AndroidRuntime(20895): at dalvik.system.NativeStart.main(Native Method)
当我注释掉将可见性更改为可见的行时,我没有得到异常.
我首先想到异常是由一些其他代码迭代通过一个hashmap引起的,但是,我在迭代我使用的hashmaps时没有做任何修改,也没有多线程,这似乎是最常见的原因.这个例外.当我不改变能见度时,我也没有得到例外.
编辑:
自定义片段中发生异常.下面是我遍历hashmap(mWidgetConfig)的代码,其中包含有关我尝试恢复的自定义窗口小部件配置的信息. hashmap是片段中的公共变量.
在由片段创建的OnDragListener中,我根据某个拖动操作更新了hashmap,如下所示:
// Update the widget configuration of the fragment that created this listener mFragment.mWidgetConfig.put(startCircleTag,"0");
我还迭代了hashmap来检查某个条件,但是在迭代期间我没有做任何修改:
Iterator<String> keySetItr = mFragment.mWidgetConfig.keySet().iterator(); while(keySetItr.hasNext()) { String tag = keySetItr.next(); if(mFragment.mWidgetConfig.get(tag).equals((String) destSocket.getTag())) { // do something,though no modification of the hashmap break; } }
另外,在尝试恢复小部件配置时,我在片段本身中进行了一次迭代.下面是我根据hashmap配置窗口小部件的代码:
public void configureWidgets() { resetWidgets(); Iterator<String> keySetItr = mWidgetConfig.keySet().iterator(); while(keySetItr.hasNext()) { String tag = keySetItr.next(); Integer value = Integer.parseInt(mWidgetConfig.get(tag)); ImageView destSocket = null; switch(value) { case 0: // The circle will not be connected to any socket continue; case 1: destSocket = mSocket1; break; case 2: destSocket = mSocket2; break; case 3: destSocket = mSocket3; break; } ImageView startCircle = (ImageView) mLayout.findViewWithTag(tag); ImageView startPlug = (ImageView) mLayout.findViewWithTag(tag + "_plug"); // Replace the drawable of destSocket destSocket.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.socket_plugged)); // Hide plug view startPlug.setVisibility(View.INVISIBLE); // Draw a line between the start circle view and the destination socket view mConnectionLinesView.addLine(startCircle,destSocket); } } public void resetWidgets() { // Remove all lines mConnectionLinesView.removeLines(); // Show all eventually prevIoUsly hidden plugs //mPlug1.setVisibility(View.VISIBLE); //mPlug2.setVisibility(View.VISIBLE); //mPlug3.setVisibility(View.VISIBLE); // Set to backround drawable of the socket to the initial one mSocket1.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.socket).mutate()); mSocket2.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.socket).mutate()); mSocket3.setBackgroundDrawable(getActivity().getResources().getDrawable(R.drawable.socket).mutate()); }
一旦在代码中使用了设置上面“插件”可见性的行,我就会得到异常.
解
抛出异常的原因是我在OnDragListener的DragEvent.ACTION_DRAG_ENDED case语句中调用了配置方法.当我将相同的代码放入DragEvent.ACTION_DROP case语句时,不会抛出异常.不知道为什么.谢谢你的帮助
解决方法
拖动开始时,ViewGroup会创建一个子视图的HashSet,必须通知ACTION_DRAG_ENDED事件.这是一组可见的孩子.当子可见性发生变化时,相应的ViewGroup会修改该集合(如果子集的可见性是可见的,则添加子集).在您的情况下,它会在迭代过程中发生.
想想,最简单的解决方案是推迟可见性变化.
view.post(new Runnable() { public void run() { view.setVisibility(View.VISIBLE); } });
将代码放入ACTION_DROP case语句时不会发生异常,因为在更改视图可见性时不会迭代该集合.
有关详细信息,请参阅ViewGroup.dispatchDragEvent(DragEvent)源代码.
今天的关于如何在Android中处理ConcurrentModificationException和android.intent.action.media_mounted的分享已经结束,谢谢您的关注,如果想了解更多关于android – Drag’n’Drop ConcurentModificationException、android – java.util.ConcurrentModificationException和SharedPreference、android – java.util.ConcurrentModificationException在MapView上、Android – setVisibility导致java.util.ConcurrentModificationException的相关知识,请在本站进行查询。
本文标签: