2013-05-17 12 views
5

Normalmente un errore mentre si fa qualcosa sul thread dell'interfaccia utente da un altro thread ho pensato, ma non capisco cosa Sto sbagliando L'errore sembra apparire solo quando il telefono sta viaggiando, quindi con una posizione GPS che cambia.LocationManager: java.lang.RuntimeException: impossibile creare il gestore all'interno del thread che non ha chiamato Looper.prepare()

Desidero memorizzare la posizione più recente, quindi nulla sull'interfaccia utente. Ho il seguente metodo chiamato dall'attività principale:

public void getFreshDeviceLocation(long interval, final long maxtime) { 
      if (gps_recorder_running){return;} 
       gpsTimer = new Timer(); 
      //starts location 
      startMillis=System.currentTimeMillis(); 

      // receive updates   
      for (String s : locationManager.getAllProviders()) { 

         LocationListener listener=new LocationListener() { 


          @Override 
          public void onProviderEnabled(String provider) { 

          } 

          @Override 
          public void onProviderDisabled(String provider) { 

          } 

          @Override 
          public void onLocationChanged(Location location) { 
           // if this is a gps location, we can use it 
           if (location.getProvider().equals(
             LocationManager.GPS_PROVIDER)) { 
            doLocationUpdate(location, true); //stores the location in the prefs 
            stopGPS(); 
           } 
          } 

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

          } 
         }; 

         locationManager.requestLocationUpdates(s, interval, //line 97, error! 
           minDistance, listener); 
         myListeners.add(listener); 

        gps_recorder_running = true; 
      } 

      // start the gps receiver thread 
      gpsTimer.scheduleAtFixedRate(new TimerTask() { 

       @Override 
       public void run() { 
        Location location = getBestLocation(); 
      doLocationUpdate(location, false); 
      if ((System.currentTimeMillis()-startMillis)>maxtime){if (maxtime>0){stopGPS();}} 
//Updates depend on speed of the device 
      float speed=pref.DeviceLocation().getSpeed(); 
       if (speed<1){if (speedclass!=0){speedclass=0;stopGPS();getFreshDeviceLocation(300000,0);}} 
       if ((speed>=1)&&(speed<3)){if (speedclass!=1){speedclass=1;stopGPS();getFreshDeviceLocation(90000,0);}} 
       if ((speed>=3)&&(speed<17)){if (speedclass!=2){speedclass=2;stopGPS();getFreshDeviceLocation(15000,0);}} 
       if (speed>=17){if (speedclass!=3){speedclass=3;stopGPS();getFreshDeviceLocation(10000,0);}} 
       } 
      }, 0, interval); 

     } 

L'errore che ottengo è:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
at android.os.Handler.<init>(Handler.java:121) 
at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:139) 
at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:137) 
at android.location.LocationManager._requestLocationUpdates(LocationManager.java:708) 
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:630) 
at com.appiclife.tmoflashlight.TMO_LocationManager.getFreshDeviceLocation(TMO_LocationManager.java:97) 
at com.appiclife.tmoflashlight.TMO_LocationManager$2.run(TMO_LocationManager.java:116) 
at java.util.Timer$TimerImpl.run(Timer.java:289) 

Linea 97 = locationManager.requestLocationUpdates (s, intervallo, minDistance, ascoltatore); (Classe LocationManager)

Sembra che devo chiamare Looper.prepare(); e Looper.loop(); da qualche parte, ma non vedo dove? Che potrebbe avere qualcosa a che fare con questa domanda AsyncTask and Looper.prepare() error

+1

Non vedo una chiamata a 'Looper.prepare()' ... – Collin

+0

Dove dovrei chiamarlo? – Diego

risposta

8

In questo caso, penso che quello che vi serve è un filo del crochet, this possono aiutare.

Oppure è possibile creare un gestore all'interno del metodo run(), rendendolo un thread looper come nell'esempio seguente.

Handler mHandler; 

public void run(){ 
    Looper.prepare(); 
    mHandler = new Handler(){ 
    public void handleMessage(Message msg){ 
     Looper.myLooper().quit(); 
    }  
    }; 
    //do your stuff 
    //... 
    mHandler.sendEmptyMessage(0); //send ourself a message so the looper can stop itself 
    Looper.loop(); 
}//end of run 

Per favore fatemi sapere se aiuta :)

+0

Non funziona. Ho ottenuto 2 errori di compilazione. 'Looper.quit()' e 'mHandler.sendMessage (0);'. Invece la risposta di jagdish funziona come un incantesimo. – Niklas

+0

@Niklas Spiacente, qual è l'errore di compilazione? (Ho perso prima) – Chen

+0

** Looper.quit(); ** 'Impossibile creare un riferimento statico al metodo non statico quit() dal tipo Looper. ** mHandler.sendMessage (0); ** 'Il metodo sendMessage (Message) nel gestore del tipo non è applicabile per gli argomenti int' – Niklas

3
 runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      alert("Error: " + message); 
     } 
    }); 

È possibile utilizzare semplicemente runOnUiThread per risolvere questo problema. Grazie !!

Problemi correlati