繁体   English   中英

如何在android中解决这个错误 - java.lang.RuntimeException:每个线程只能创建一个Looper

[英]How to resolve this error in android - java.lang.RuntimeException: Only one Looper may be created per thread

这是我在后台服务中的代码

public class BackgroundService extends Service {
    private int interval1 = 60; // 60 seconds
    private int interval2 = 60; // 60 seconds
    //=============================================================================================================================
    private Handler mTimer1 = new Handler();
    private Runnable mTask1 = new Runnable() {
        public void run() {
            Looper.prepare();
            Log.w("GPS Tracker", "Tracker going to run "+new Date());
            LocationManager mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
            LocationListener mlocListener = new MyLocationListener();
            mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);//, Looper.getMainLooper()); 
            mTimer1.postDelayed(this, interval1 * 1000L);
            Looper.loop(); 
        }
    };


//=============================================================================================================================
    private Handler mTimer2 = new Handler();
    private Runnable mTask2 = new Runnable() {
        public void run() {
            if (isOnline() == true) {
                Log.w("Method 2", "setUpdateCardAcceptTag");
                setUpdateCardAcceptTag();
                Log.w("Method 3", "getCardBulkSerialData");
                getCardBulkSerialData();
                Log.w("Method 12", "updateStockDataFromRemote");
                updateStockDataFromRemote();
                Log.w("Method 5", "SetRemarksData");
                SetRemarksData();
                Log.w("Method 6", "SetCardSaleData");
                SetCardSaleData();
                Log.w("Method 7", "synchMerchants");
                synchMerchants();
                Log.w("Method 9", "getUpdatedCities");
                getUpdatedCities();
                Log.w("Method 10", "getNotifications");
                getNotifications();
                Log.w("Method 11", "getNextSerialDetails");
                getNextSerialDetails();
                Log.w("Method 12", "getNextSerialDetails");
                //synchLocations();
                Log.w("Method 13", "synchLocations");

            }
            mTimer2.postDelayed(this, interval2 * 1000L);
        }
    };

当我运行应用程序时,它在几秒钟后停止。在日志控制台中说下面的错误消息请帮我解决这个问题

谢谢

您无法多次为Thread准备Looper 在准备之前,Chcek是否Looper与该Thread相关联。

private Runnable mTask1 = new Runnable() {
    public void run() {
        if(Looper.myLooper() == null) { // check already Looper is associated or not.
           Looper.prepare(); // No Looper is defined So define a new one
        }
        Log.w("GPS Tracker", "Tracker going to run "+new Date());
        LocationManager mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);//, Looper.getMainLooper()); 
        mTimer1.postDelayed(this, interval1 * 1000L);
        Looper.loop(); 
    }
};

因为当一个活动或服务(它们都扩展了ContextWrapper ,扩展了Context )是由android创建的时候,也会创建一个Loopper对象。

在您的代码中,您使用Looper.prepare(); 但它真正做到了什么?

public static void prepare() {
    prepare(true);
}

private static void prepare(boolean quitAllowed) {
    if (sThreadLocal.get() != null) {
        throw new RuntimeException("Only one Looper may be created per thread");
    }
    sThreadLocal.set(new Looper(quitAllowed));
}

(你可以在Looper.java中找到这段代码)

因为android os已经调用了mLooper.prepare(); 创建服务时的方法,因此sThreadLocal有一个Looper对象。当您的代码执行时,将抛出RuntimeException

Handler在创建Handler的线程中运行'runnable'。 你有2个处理程序..它们在同一个线程中。 我猜他们在主线程..并且主线程已经有一个looper ..尝试删除“Looper.Prepare”和“Looper.Loop”调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM