2013-05-17 17 views
5

Thông thường một lỗi trong khi thực hiện điều gì đó trên chuỗi giao diện người dùng từ một chuỗi khác mà tôi nghĩ, nhưng tôi không hiểu Tôi đang làm sai. Lỗi dường như chỉ xuất hiện khi điện thoại di chuyển, vì vậy với vị trí GPS thay đổi.LocationManager: java.lang.RuntimeException: Không thể tạo trình xử lý bên trong luồng mà không được gọi là Looper.prepare()

Tôi muốn vị trí gần đây nhất được lưu trữ, do đó không có gì trên giao diện người dùng. Tôi có phương pháp sau đây gọi từ các hoạt động chính:

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

     } 

Các lỗi tôi nhận được là:

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) 

Dòng 97 = locationManager.requestLocationUpdates (s, khoảng cách, minDistance, nghe); (Lớp LocationManager)

Có vẻ như tôi phải gọi Looper.prepare(); và Looper.loop(); ở đâu đó, nhưng tôi không thấy ở đâu? Có thể có điều gì đó liên quan đến câu hỏi này AsyncTask and Looper.prepare() error

+1

Tôi không thấy cuộc gọi đến 'Looper.prepare()' ... – Collin

+0

Tôi nên gọi điện thoại ở đâu? – Diego

Trả lời

8

Trong trường hợp này, tôi nghĩ rằng những gì bạn cần là chủ đề looper, this có thể giúp bạn.

Hoặc bạn có thể tạo một trình xử lý bên trong phương thức run() của bạn làm cho nó trở thành một chủ đề looper như ví dụ sau.

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 

Xin vui lòng cho tôi biết nếu nó giúp :)

+0

Không hoạt động. Tôi có 2 lỗi biên dịch. 'Looper.quit()' và 'mHandler.sendMessage (0);'. Thay vào đó, câu trả lời của các tác phẩm jagdish giống như một sự quyến rũ. – Niklas

+0

@Niklas Xin lỗi, lỗi biên dịch là gì? (Tôi đã bỏ lỡ trước đây) – Chen

+0

** Looper.quit(); ** 'Không thể thực hiện tham chiếu tĩnh đối với phương thức tĩnh() từ kiểu Looper.' ** mHandler.sendMessage (0); ** 'Phương thức sendMessage (Message) trong trình xử lý kiểu không áp dụng cho các đối số int' – Niklas

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

Bạn có thể sử dụng đơn giản runOnUiThread để khắc phục vấn đề này. Cảm ơn !!

Các vấn đề liên quan