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

如何在android-java.lang.RuntimeException中解决此错误:每个线程只能创建一个循环器,java,android,Java,Android,这是我在后台服务中的代码 public class BackgroundService extends Service { private int interval1 = 60; // 60 seconds private int interval2 = 60; // 60 seconds //==========================================================================================

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

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);
        }
    };
当我运行该应用程序时,它在几秒钟后停止。它在日志控制台中显示以下错误消息 请帮我解决这个问题


谢谢

您不能为
线程
多次准备
活套
。在准备之前,检查
活套
是否与该
线程
关联

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(); 
    }
};

处理程序在创建处理程序的线程中运行“runnable”。您有两个处理程序。。他们在同一条线上。我想他们是在主线上。。主线程已经有了一个活套。。尝试删除“Looper.Prepare”和“Looper.Loop”调用

因为当android创建活动或服务(它们都扩展了
ContextWrapper
,扩展了
Context
)时,也会创建一个
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操作系统已经调用了
mLooper.prepare()方法,因此
sThreadLocal
有一个Looper对象。执行代码时,将抛出
RuntimeException

谢谢亲爱的Gopal Rao。现在它已经工作了。但当我通过emulator.it给longlatt时,它被添加到表中两次。如何纠正这个问题。?