簡體   English   中英

Maintask中run方法中的Android異常

[英]Android Exception in run Method in a Maintask

java.lang.RuntimeException:無法在尚未調用Looper.prepare()的線程內創建處理程序

我有一個服務,每15秒調用run MainTaskrun方法(出於測試原因) timer.scheduleAtFixedRate(new MainTask(), 0, 15000); (在onStartCommandonStartCommand

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!
        }

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

    }

獲得位置后,此方法稱為: mLocationManager.removeUpdates(this);

什么時候發生此錯誤? 第四次跑步被稱為

可能的答案需要包含什么? 一個答案應該可以幫助我在不更改任何邏輯行為的情況下解決此錯誤,並且不應在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