2011-11-03 39 views
0

這裏是我得到Android的致命異常定時器0

11-03 03:03:02.380: W/dalvikvm(1381): threadid=9: thread exiting with uncaught exception (group=0x40015560) 
11-03 03:03:02.400: E/AndroidRuntime(1381): FATAL EXCEPTION: Timer-0 
11-03 03:03:02.400: E/AndroidRuntime(1381): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
11-03 03:03:02.400: E/AndroidRuntime(1381):  at android.os.Handler.<init>(Handler.java:121) 
11-03 03:03:02.400: E/AndroidRuntime(1381):  at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:173) 
11-03 03:03:02.400: E/AndroidRuntime(1381):  at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:173) 
11-03 03:03:02.400: E/AndroidRuntime(1381):  at android.location.LocationManager._requestLocationUpdates(LocationManager.java:579) 
11-03 03:03:02.400: E/AndroidRuntime(1381):  at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446) 
11-03 03:03:02.400: E/AndroidRuntime(1381):  at com.colinlgray.MyService$1.run(MyService.java:84) 
11-03 03:03:02.400: E/AndroidRuntime(1381):  at java.util.Timer$TimerImpl.run(Timer.java:284) 

這裏的錯誤是我的服務,我想用一個定時器運行它內部的更新,我正打算使用GPS定位發送到網絡服務器

package com.colinlgray; 

import android.app.Service; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

import java.util.Timer; 
import java.util.TimerTask; 

import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import com.google.android.maps.GeoPoint; 

import android.content.Context; 

public class MyService extends Service{ 
int counter = 0; 
static final int UPDATE_INTERVAL = 1000; 
private Timer timer = new Timer(); 

GeoPoint geopoint; 

private LocationManager lm; 
private LocationListener locationListener; 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 


@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // We want this service to continue running until it is explicitly 
    // stopped, so return sticky. 
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); 
    doSomethingRepeatedly(); 
    return START_STICKY; 
} 

private void doSomethingRepeatedly() { 
    timer.scheduleAtFixedRate(new TimerTask() { 
     public void run() { 
      lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

      class MyLocationListener implements LocationListener{ 

       public void onLocationChanged(Location location) { 
        geopoint = new GeoPoint(
          (int) (location.getLatitude() * 1E6), 
          (int) (location.getLongitude() * 1E6)); 
        double latitude = location.getLatitude(); 
        double longitude = location.getLatitude(); 
        double altitude = location.getAltitude(); 
        double accuracy = location.getAccuracy(); 
        Log.d("LONGITUDE", String.valueOf(longitude)); 
        Log.d("LATITTUDE", String.valueOf(latitude)); 

       } 

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

       } 

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

       } 

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

       } 
      } 

      locationListener= new MyLocationListener(); 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); 
     } 
    }, 0, UPDATE_INTERVAL); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    if (timer != null){ 
     timer.cancel(); 
    } 
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); 
} 

} 

我將不勝感激任何幫助或建議。謝謝

回答

2

我有和你一樣的問題。 我已經解決了在定時器中執行locationrequestupdates以及在定時器I的運行方法中向網絡服務器發送緯度和經度的問題。

其他可能的解決方法是不要使用定時器。隨着

public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener) 

你可以告訴新更新位置之間的「onLocationChanged(位置定位)」,您可以發送新位置minTime。

+0

非常感謝! –

1

異步任務將從UI線程中調用。任何沒有調用Looper.prepare()的線程都會導致這個問題。

今天我遇到了同樣的問題,所以張貼我的工作(s),看看這是最好的解決方案(s)。

都在你的代碼首先不做這裏面運行

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

移動而不是這dosomethingrepeatedly啓動的功能。 例如

public void dosomethingrepeatedly() 
     { 
      final Myclass x = new Myclass(); 
      .......... 
     public void run() 
     { 
      x.somefunction() 
     } 
     } 

另一種方法: Findout的currentThread

Thread.currentThread().getClass(); 

如果定時器的一個實例可以調用Looper.prepare(); 設置一個標誌,以便Looper.prepare只被調用一次。 例如

boolean looperCalled=false; 
    ... 
    ... 
    ... 
    public void dorepeatedly() 
    { 
     .... 
     .... 
     public void run() 
     { 
      if(Thread.currentThread().getName().equals(java.util.Timer$TimerImpl) 
      { 
       if(looperCalled){//do nothing} 
       else {Looper.prepare(); looperCalled=true;} 
      } 
     } 
    } 
+0

對此款式表示歉意, – Orlymee

+0

對此有何反饋?它確實解決了錯誤 – Orlymee

+0

它可能是你的答案的風格,請嘗試改善這種編輯風格 – frayab