2013-05-17 10 views
5

通常、UIスレッドで何かしている間にエラーが発生しました。私は間違っている。このエラーは、携帯電話が移動しているときにのみ表示されるように見えます。LocationManager:java.lang.RuntimeException:Looper.prepare()を呼び出していないスレッド内でハンドラを作成できません

最新の場所を保存したいので、UIには何も保存しません。私は主な活動から、次のメソッドを呼び出しています

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

     } 

私が手にエラーがある:

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) 

ライン97 = locationManager.requestLocationUpdates(S、間隔、minDistance、リスナー)。 (LocationManagerクラス)

私はLooper.prepare()を呼び出さなければならないようです。とLooper.loop();どこかに見えますが、私はどこに見えませんか?それはこの質問と関係があるかもしれませんAsyncTask and Looper.prepare() error

+1

私は 'Looper.prepare()'の呼び出しを見ません... – Collin

+0

どこで呼びますか? – Diego

答えて

8

この場合、私はあなたが必要と思うのはルーパースレッドであると思いますが、thisが役に立ちます。

または、run()メソッド内にハンドラを作成して、次の例のようにルーパスレッドにすることができます。

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 

それが助け場合は私に知らせてください:)

+0

動作しません。私は2つのコンパイルエラーがあります。 'Looper.quit()'と 'mHandler.sendMessage(0);'があります。代わりにジャグジーの答えは魅力のように働きます。 – Niklas

+0

@Niklas申し訳ありません、コンパイルエラーは何ですか? (私は前に逃しました) – Chen

+0

** Looper.quit(); ** 'Looper型から非静的メソッドquit()への静的な参照を作成できません。 ' ** mHandler.sendMessage(0); ** '型ハンドラのメソッドsendMessage(Message)は、引数int { – Niklas

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

あなたがこの問題を解決するために、単純runOnUiThreadを使用することができます。 ありがとうございます!

関連する問題