java.lang.IllegalStateException:没有在主线程 [英] java.lang.IllegalStateException: Not on the main thread

查看:315
本文介绍了java.lang.IllegalStateException:没有在主线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除从Goolge的地图在地图FragmentActivity标记时,有没有在表中的服务器端制造商提供的数据也数据对象是空的,但我得到下面的错误。我怎样才能解决这个问题?

错误:

  20 07-12:53:05.697:E / AndroidRuntime(26364):致命异常:IntentService [IntentService]
07-12 20:53:05.697:E / AndroidRuntime(26364):工艺:com.bustracker,PID:26364
07-12 20:53:05.697:E / AndroidRuntime(26364):java.lang.IllegalStateException:没有在主线程
07-12 20:53:05.697:E / AndroidRuntime(26364):在com.google.l.a.ce.b(来源不明)
07-12 20:53:05.697:E / AndroidRuntime(26364):在com.google.maps.api.android.lib6.d.ct.a(来源不明)
07-12 20:53:05.697:E / AndroidRuntime(26364):在com.google.maps.api.android.lib6.d.aq.a(来源不明)
07-12 20:53:05.697:E / AndroidRuntime(26364):在com.google.android.gms.maps.model.internal.t.onTransact(的SourceFile:51)
07-12 20:53:05.697:E / AndroidRuntime(26364):在android.os.Binder.transact(Binder.java:380)
07-12 20:53:05.697:E / AndroidRuntime(26364):在com.google.android.gms.maps.model.internal.zzi $ ZZA $ zza.remove(来源不明)
07-12 20:53:05.697:E / AndroidRuntime(26364):在com.google.android.gms.maps.model.Marker.remove(来源不明)
07-12 20:53:05.697:E / AndroidRuntime(26364):在com.bustracker.GetLLRD.onHandleIntent(GetLLRD.java:120)
07-12 20:53:05.697:E / AndroidRuntime(26364):在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65)
07-12 20:53:05.697:E / AndroidRuntime(26364):在android.os.Handler.dispatchMessage(Handler.java:102)
07-12 20:53:05.697:E / AndroidRuntime(26364):在android.os.Looper.loop(Looper.java:145)
07-12 20:53:05.697:E / AndroidRuntime(26364):在android.os.HandlerThread.run(HandlerThread.java:61)

在GetLLRD IntentService类的 onHandleIntent方式:

 保护无效onHandleIntent(意向意图){                            如果(数据= NULL&放大器;!&安培;!data.isEmpty()){
                                意图int​​ent1 =新意图(这一点,Map.class);
                                intent1.putExtra(list_data数据);
                                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                startActivity(intent1);                            }否则如果(Map.markerMap!= NULL
                                    &功放;&安培; !Map.markerMap.isEmpty()){                                迭代器< HashMap.Entry<整数,标记>>它= Map.markerMap
                                        .entrySet()迭代器()。
                                而(it.hasNext()){
                                    HashMap.Entry<整数,标记> entery =它
                                            。下一个();
                                    INT键= entery.getKey();
                                    Map.marker = Map.markerMap.get(键);
                                    的System.out.println(标志测试+ Map.marker);
                                    // 120线。
                                    Map.marker一个.remove();
                                    Map.markerMap.remove(键);                                    //标记值= entery.getValue();
                                }
                            }

地图fragmentActivity:

 公共类地图扩展FragmentActivity实现OnMa $ P $ {padyCallback    GoogleMap的地图;
    静态HashMap的<整数,标记> markerMap =新的HashMap<整数,标记>();
    静态标记标记= NULL;    ...
    }


解决方案

一个速战速决的例外是把地图更新在的Runnable 来的执行主线程(低于code)。然而,真正的问题的答案是,你需要重新考虑你的设计。它是不安全的在 IntentService code在活动使用静态数据。什么是使用的原因 IntentService ?地图更新必须在主线程上运行。其中的一个目的 IntentService 是要执行的操作不能的被主线程上完成,并且需要一个后台线程。为什么要使用 IntentService 时,它包含有code需要主线程?

 新的处理程序(Looper.getMainLooper())。后(新的Runnable(){
        @覆盖
        公共无效的run(){
            迭代器< HashMap.Entry<整数,标记>>它= Map.markerMap
                    .entrySet()迭代器()。
            而(it.hasNext()){
                HashMap.Entry<整数,标记> entery =它
                        。下一个();
                INT键= entery.getKey();
                Map.marker = Map.markerMap.get(键);
                的System.out.println(标志测试+ Map.marker);
                // 120线。
                Map.marker.remove();
                //从地图中使用迭代器删除
                it.remove();                //标记值= entery.getValue();
            }
        }
    });

I am trying to delete the markers from the Goolge map in the FragmentActivity Map when there is no data available in the table for the makers on the serverside also the data object is empty but I am getting the error below. How can I fix it?

Error:

07-12 20:53:05.697: E/AndroidRuntime(26364): FATAL EXCEPTION: IntentService[IntentService]
07-12 20:53:05.697: E/AndroidRuntime(26364): Process: com.bustracker, PID: 26364
07-12 20:53:05.697: E/AndroidRuntime(26364): java.lang.IllegalStateException: Not on the main thread
07-12 20:53:05.697: E/AndroidRuntime(26364):    at com.google.l.a.ce.b(Unknown Source)
07-12 20:53:05.697: E/AndroidRuntime(26364):    at com.google.maps.api.android.lib6.d.ct.a(Unknown Source)
07-12 20:53:05.697: E/AndroidRuntime(26364):    at com.google.maps.api.android.lib6.d.aq.a(Unknown Source)
07-12 20:53:05.697: E/AndroidRuntime(26364):    at com.google.android.gms.maps.model.internal.t.onTransact(SourceFile:51)
07-12 20:53:05.697: E/AndroidRuntime(26364):    at android.os.Binder.transact(Binder.java:380)
07-12 20:53:05.697: E/AndroidRuntime(26364):    at com.google.android.gms.maps.model.internal.zzi$zza$zza.remove(Unknown Source)
07-12 20:53:05.697: E/AndroidRuntime(26364):    at com.google.android.gms.maps.model.Marker.remove(Unknown Source)
07-12 20:53:05.697: E/AndroidRuntime(26364):    at com.bustracker.GetLLRD.onHandleIntent(GetLLRD.java:120)
07-12 20:53:05.697: E/AndroidRuntime(26364):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
07-12 20:53:05.697: E/AndroidRuntime(26364):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-12 20:53:05.697: E/AndroidRuntime(26364):    at android.os.Looper.loop(Looper.java:145)
07-12 20:53:05.697: E/AndroidRuntime(26364):    at android.os.HandlerThread.run(HandlerThread.java:61)

onHandleIntent method in the GetLLRD IntentService class:

protected void onHandleIntent(Intent intent) {

                            if (data != null && !data.isEmpty()) {
                                Intent intent1 = new Intent(this, Map.class);
                                intent1.putExtra("list_data", data);
                                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                startActivity(intent1);

                            } else if (Map.markerMap != null
                                    && !Map.markerMap.isEmpty()) {

                                Iterator<HashMap.Entry<Integer, Marker>> it = Map.markerMap
                                        .entrySet().iterator();
                                while (it.hasNext()) {
                                    HashMap.Entry<Integer, Marker> entery = it
                                            .next();
                                    int key = entery.getKey();
                                    Map.marker = Map.markerMap.get(key);
                                    System.out.println("test marker " + Map.marker ); 
                                    //Line 120.
                                    Map.marker .remove();
                                    Map.markerMap.remove(key);

                                    // Marker value = entery.getValue();
                                }
                            }

Map fragmentActivity:

public class Map extends FragmentActivity implements OnMapReadyCallback {

    GoogleMap map;
    static HashMap<Integer, Marker> markerMap = new HashMap<Integer, Marker>();
    static Marker marker = null;

    ...
    }

解决方案

A quick fix for the exception would be to put the map updates in a Runnable to execute on the main thread (code below). However, the real answer to the question is that you need to re-think your design. It is unsafe for code in an IntentService to be using static data in an activity. What are the reasons for using an IntentService? The map updates must run on the main thread. One of the purposes of an IntentService is to perform operations that cannot be done on the main thread, and need a background thread. Why use an IntentService when it contains has code that needs the main thread?

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            Iterator<HashMap.Entry<Integer, Marker>> it = Map.markerMap
                    .entrySet().iterator();
            while (it.hasNext()) {
                HashMap.Entry<Integer, Marker> entery = it
                        .next();
                int key = entery.getKey();
                Map.marker = Map.markerMap.get(key);
                System.out.println("test marker " + Map.marker );
                //Line 120.
                Map.marker.remove();
                // Remove from map using iterator
                it.remove();

                // Marker value = entery.getValue();
            }
        }
    });

这篇关于java.lang.IllegalStateException:没有在主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆