简体   繁体   English

Maintask中run方法中的Android异常

[英]Android Exception in run Method in a Maintask

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() java.lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序

I got a service calling the run method of my MainTask every 15 seconds (for testing reasons) timer.scheduleAtFixedRate(new MainTask(), 0, 15000); 我有一个服务,每15秒调用run MainTaskrun方法(出于测试原因) timer.scheduleAtFixedRate(new MainTask(), 0, 15000); (called in onStartCommand . (在onStartCommandonStartCommand

The code in the run method is looking like this: run方法中的代码如下所示:

public void run() {
        Log.v("MainTask", "run() Called");
        GpsHelper gpsHelper = new GpsHelper(getApplicationContext(), this); // This is causing the error - commented out => no error
        // Doing some independent webrequests here!
        }

The GpsHelper class is looking like this (it has some if statements - because it is also used outside the Service in the Mainactivity : GpsHelper类看起来像这样(它具有一些if语句-因为它也在Mainactivity的Service外部使用:

public GpsHelper(Context context, LocationReceiver locationReceiver) {
    this.context = context;
    this.locationReceiver = locationReceiver;
    mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    if (locationReceiver instanceof Runnable) {
        isRunnable = true;
    }
    run();
}

/**
 * Getting the Location, handling permission, requesting location updates if time is greater than 2 minutes
 */
public void getLocation() {
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && !isRunnable) {
        StaticHelpers.askForFineLocationPermission(context);
    } else {
        if (isRunnable && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.v("GpsHelper", "Runnable can't get position - check permissions!");
        } else {
            Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            // If time not greater than 2 minutes
            // TODO: Move to resources
            if (location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
                Log.v("GpsHelper", "Using existing location");
                locationReceiver.onLocationAvailable(location);
            } else {
                Log.v("GpsHelper", "No Position Available => Requesting Location Updates");
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
                if (!isRunnable && !mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    showSettingsDialog();
                }
            }
        }

    }

After getting the position this method is called: mLocationManager.removeUpdates(this); 获得位置后,此方法称为: mLocationManager.removeUpdates(this);

When is this error occurring? 什么时候发生此错误? The fourth time run is getting called 第四次跑步被称为

What does an possible answer need to contain? 可能的答案需要包含什么? An answer should help me to fix this error without changing any logical behaviour and should not run on the ui thread ! 一个答案应该可以帮助我在不更改任何逻辑行为的情况下解决此错误,并且不应在ui线程上运行

getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Log.v("MainTask", "run() Called");
            GpsHelper gpsHelper = new GpsHelper(getApplicationContext(), this); // This is causing the error - commented out => no error
        }
    });

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

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