Android:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

时间:2015-09-01 07:05:00

标签: android runnable locationmanager android-handler

我正在创建一个Android应用程序,我正在尝试使用位置管理器获取位置,然后我将该位置推送到服务器。当我尝试这样做时,我收到以下错误。

size_t arr_size = 0;
scanf("%zu", &arr_size);

这是我的appLocationService

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
            at android.os.Handler.<init>(Handler.java:200)
            at android.os.Handler.<init>(Handler.java:114)
            at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:223)
            at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:223)
            at android.location.LocationManager.wrapListener(LocationManager.java:851)
            at android.location.LocationManager.requestLocationUpdates(LocationManager.java:864)
            at android.location.LocationManager.requestLocationUpdates(LocationManager.java:459)
            at com.pickingo.fe.services.gps.AppLocationService.getLocation(AppLocationService.java:30)
            at com.pickingo.fe.gps.CurrentLocationPusher.run(CurrentLocationPusher.java:60)
            at java.lang.Thread.run(Thread.java:818)

这是我从LocationManager获取位置后用于推送位置的代码。

public class AppLocationService extends Service implements LocationListener {

    protected LocationManager locationManager;
    Location location;

    private static final long MIN_DISTANCE_FOR_UPDATE = 50;
    private static final long MIN_TIME_FOR_UPDATE = 1000*60*2;

    public AppLocationService(Context context) {
        locationManager = (LocationManager) context
                .getSystemService(LOCATION_SERVICE);
    }

    public Location getLocation(String provider) {
        if (locationManager.isProviderEnabled(provider)) {
            locationManager.requestLocationUpdates(provider,
                    MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(provider);
                return location;
            }
        }
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

}

我在CurrentLocationPusher的以下行收到错误

public class CurrentLocationPusher implements Runnable {


    private static CurrentLocationPusher _singleInstance;

    private CurrentLocationPusher() {
        this.apiClient = ApiClient.getInstance();
        this.appLocationService = new AppLocationService(appContext);
        this.runningThread = new Thread(this);
    }

    public static CurrentLocationPusher getInstance() {
        if (_singleInstance == null)
            synchronized (CurrentLocationPusher.class) {
                if (_singleInstance == null)
                    _singleInstance = new CurrentLocationPusher();
            }
        return _singleInstance;
    }

    private boolean isStopThread = false;

    public static final long ONE_MINUTE = 60 * 1000;

    private static Context appContext;

    private AppLocationService appLocationService;

    private ApiClient apiClient;
    private Thread runningThread;


    public static void init(Context applicationContext) {

        appContext = applicationContext;
    }

    @Override
    public void run() {

        Location location = appLocationService
                .getLocation(LocationManager.GPS_PROVIDER);
        while (!isStopThread) {
            try {
                if (location != null)
                    if (CommonUtils.isActiveToInternet(appContext))
                        apiClient.getApi(appContext).pushLocation(String.valueOf(location.getLatitude()),
                                String.valueOf(location.getLongitude()),String.valueOf(System.currentTimeMillis()),
                                String.valueOf(location.getAccuracy()));
                Thread.sleep(ONE_MINUTE);
            } catch (InterruptedException e) {

            } catch (Exception e) {
                Log.e(PickingoConstant.TAG, "error" + e.getMessage());
            }
        }

    }


    public void startPushingLocation() {
        if (this.runningThread.getState() == Thread.State.NEW) {
            runningThread.start();
            isStopThread = false;
        } else if (this.runningThread.getState() == Thread.State.TERMINATED) {
            (runningThread = new Thread(this)).start();
            isStopThread = false;
        } else if (this.runningThread.getState() == Thread.State.RUNNABLE) {
            return;
        }

    }

    public void stopPushingLocation() {
        isStopThread = true;
    }
}

如果有人可以给出提示或帮助,我会非常感激。在此先感谢!!

1 个答案:

答案 0 :(得分:1)

正如迈克尔所指出的那样,在我的run()方法开始时添加了这段代码。

if (Looper.myLooper() == null) {
            Looper.prepare();
        }