0

線程我正在寫一個程序,每5分鐘提取用戶當前的經度和緯度。我使用AlarmManager,我想運行的程序的背景,所以我用IntentService。但在執行程序時會出現以下錯誤:引起:java.lang.RuntimeException:不能創建處理程序內部沒有調用Looper.prepare()

logcat的

02-06 15:40:03.101: W/dalvikvm(17499): threadid=12: thread exiting with uncaught exception (group=0x4203c2a0) 
02-06 15:40:03.109: E/AndroidRuntime(17499): java.lang.RuntimeException: An error occured while executing doInBackground() 
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.os.AsyncTask$3.done(AsyncTask.java:299) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.lang.Thread.run(Thread.java:856) 
02-06 15:40:03.109: E/AndroidRuntime(17499): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.os.Handler.<init>(Handler.java:121) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:180) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:180) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:657) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:482) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at com.gpsshoppe.LocationFinder$CurrentLocatioFinder.doInBackground(LocationFinder.java:58) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at com.gpsshoppe.LocationFinder$CurrentLocatioFinder.doInBackground(LocationFinder.java:1) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
02-06 15:40:03.109: E/AndroidRuntime(17499): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
02-06 15:40:03.109: E/AndroidRuntime(17499): ... 5 more 

這是我的課

import android.app.IntentService; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import com.google.android.gms.maps.model.LatLng; 

public class LocationFinder extends IntentService { 
    private Context mcontext; 
    SharedPreferences shprefs; 


    public LocationFinder() { 
     super("LocationFinder"); 
     this.mcontext=this; 

     // TODO Auto-generated constructor stub 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     // TODO Auto-generated method stub 
     CurrentLocatioFinder lFinder=new CurrentLocatioFinder(); 
     lFinder.execute(); 

    } 
public class CurrentLocatioFinder extends AsyncTask<String, String, String> implements LocationListener{ 


    @Override 
    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 

     LocationManager locationManager = (LocationManager) mcontext.getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 

     return null; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 
     if(location!=null){ 
     shprefs=PreferenceManager.getDefaultSharedPreferences(LocationFinder.this); 


     double latitude=location.getLatitude(); 
     Log.i("Inside cLocation", ""+latitude); 

     Editor edit=shprefs.edit(); 
     edit.putString("Ltd", ""+latitude); 
     edit.commit(); 

     double longitude=location.getLongitude(); 

     edit.putString("Lngtd", ""+longitude); 
     edit.commit(); 
     LatLng lng=new LatLng(latitude,longitude); 
     Log.i("LatLng", ""+lng); 

     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 
    } 

} 
+0

你在哪裏初始化了mcontext? –

回答

0

您已經爲AsyncTask實現中的locationservice定義了回調處理程序。處理程序將在其創建的線程中執行,因此在您的情況下,需要從您無法執行的AsyncTask線程執行位置回調。我建議你將locationservice處理程序移到你的主線程。 讓LocationFinder實現LocationListener接口。

0

我只是編輯類嘗試希望這對你的作品

public class LocationFinder extends IntentService { 
    private Context mcontext; 
    SharedPreferences shprefs; 

    public LocationFinder() { 
     super("LocationFinder"); 
     this.mcontext = this; 

     // TODO Auto-generated constructor stub 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     CurrentLocatioFinder mCurrentLocatioFinder = new CurrentLocatioFinder(); 
     mCurrentLocatioFinder.attach(); 

    } 

    public class CurrentLocatioFinder implements LocationListener { 
     LocationManager locationManager; 

     public void attach() { 
      locationManager = (LocationManager) mcontext.getSystemService(Context.LOCATION_SERVICE); 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
     } 

     @Override 
     public void onLocationChanged(Location location) { 
      // TODO Auto-generated method stub 
      if (location != null) { 
       shprefs = PreferenceManager.getDefaultSharedPreferences(LocationFinder.this); 

       double latitude = location.getLatitude(); 
       Log.i("Inside cLocation", "" + latitude); 

       Editor edit = shprefs.edit(); 
       edit.putString("Ltd", "" + latitude); 
       edit.commit(); 

       double longitude = location.getLongitude(); 

       edit.putString("Lngtd", "" + longitude); 
       edit.commit(); 
       LatLng lng = new LatLng(latitude, longitude); 
       Log.i("LatLng", "" + lng); 

      } 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 

     } 
    } 

} 
0

您不能在onHandleIntent創建的AsyncTask,因爲它的後臺線程。

我的建議是忘記IntentService,因爲在這種情況下它沒有任何增加。你可以直接啓動你的AsyncTask,不需要將它包裝在一個IntentService中。

但是如果你真的想要一個服務,它關心位置處理,那麼你可以對服務做同樣的事情,因爲它的onStartCommand方法在主線程中運行。

相關問題