2013-07-12 139 views
1

我有一個應用程序註冊locationClient更新。但是,位置偵聽器似乎永遠不會被訪問。出現「GPS連接」烤麪包,所以我知道GPS連接正常,但顯示座標的烤麪包不顯示,並且未激活監聽程序。但是,會記錄「位置更新已激活」。 「連接器」是啓動Asynctask以獲取數據庫數據的對象。它工作正常。谷歌位置服務更新忽略位置監聽器(android)

@Override 
public void onConnected(Bundle arg0) 
{ 
    Toast.makeText(context, "GPS connected", Toast.LENGTH_SHORT).show(); 
    connector = new Connector(); 
    connector.execute("clues", null, null); 
    listener = new LocationListener() 
    { 
     @Override 
     public void onLocationChanged(Location location) 
     { 
      Log.d("location", "listener activated"); 
      try 
      { 
      double[] coords = locationCoords(location); 
      if(coords[0] != 0) 
      { 
       map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(coords[0], coords[1]), (float) 14.5)); 

      } 
      Toast.makeText(context, coords[0] + ", " + coords[1], Toast.LENGTH_LONG).show(); 
      } 
      catch(NullPointerException e) 
      { 
       Log.d("nulls", "null"); 
      } 

     } 
    }; 
    mLocationClient.requestLocationUpdates(mLocationRequest, listener); 
    Log.d("location", "location updates initiated"); 
} 

這是相關位的記錄。 「map null」正在記錄一個不同的問題,那就是當我嘗試實例化它時,我的google maps片段尚未初始化。

07-12 15:15:36.608: D/dalvikvm(1116): GC_FOR_ALLOC freed 133K, 9% free 3105K/3392K, paused 46ms, total 49ms 
07-12 15:15:36.618: I/dalvikvm-heap(1116): Grow heap (frag case) to 3.757MB for 635812-byte allocation 
07-12 15:15:36.688: D/dalvikvm(1116): GC_FOR_ALLOC freed <1K, 8% free 3726K/4016K, paused 65ms, total 65ms 
07-12 15:15:36.818: D/dalvikvm(1116): GC_CONCURRENT freed 2K, 7% free 3742K/4016K, paused 8ms+6ms, total 130ms 
07-12 15:15:37.208: D/null(1116): map null 
07-12 15:15:37.228: D/location(1116): location request constructed 
07-12 15:15:37.368: D/null(1116): map null 
07-12 15:15:38.198: D/dalvikvm(1116): GC_CONCURRENT freed 141K, 7% free 3994K/4260K, paused 6ms+59ms, total 252ms 
07-12 15:15:38.478: D/libEGL(1116): loaded /system/lib/egl/libEGL_emulation.so 
07-12 15:15:38.519: D/(1116): HostConnection::get() New Host Connection established 0x2a19b220, tid 1116 
07-12 15:15:38.559: D/libEGL(1116): loaded /system/lib/egl/libGLESv1_CM_emulation.so 
07-12 15:15:38.608: D/libEGL(1116): loaded /system/lib/egl/libGLESv2_emulation.so 
07-12 15:15:39.008: W/EGL_emulation(1116): eglSurfaceAttrib not implemented 
07-12 15:15:39.068: D/OpenGLRenderer(1116): Enabling debug mode 0 
07-12 15:15:39.258: D/(1116): HostConnection::get() New Host Connection established 0x2a1b0310, tid 1140 
07-12 15:15:39.268: I/Choreographer(1116): Skipped 93 frames! The application may be doing too much work on its main thread. 
07-12 15:15:40.028: D/dalvikvm(1116): GC_FOR_ALLOC freed 345K, 12% free 4030K/4564K, paused 95ms, total 106ms 
07-12 15:15:40.398: D/dalvikvm(1116): GC_CONCURRENT freed 328K, 11% free 4179K/4696K, paused 10ms+13ms, total 130ms 
07-12 15:15:41.248: D/location(1116): location updates initiated 
07-12 15:15:41.268: D/dalvikvm(1116): GC_CONCURRENT freed 611K, 17% free 4084K/4864K, paused 7ms+61ms, total 340ms 

該日誌來自仿真器,但在真實設備上會發生相同的行爲。

+0

您使用的是SupportMapFragment或mapfragment?> – yams

+0

嗯,我想我會做出關於這個問題,全球定位系統。但是我使用了SupportMapFragment。地圖顯示OK,我不能在代碼中實例化一個代表它的對象。 – fonduman

+0

好吧,xml中的類是一個supportmapfragment,但它似乎只有片段標籤... <?xml version =「1.0」encoding =「utf-8」?> fonduman

回答

0

通常在我調用和使用GoogleMap之前,我實例化supportfragmentmanager,然後使用它來實例化地圖。像下面,我希望這有助於。

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    SupportMapFragment mf= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 


    map = mf.getMap(); 

    //map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
    ll = new SecondPointLocationListener(); 

至於你的打印位置的方法使用,而不是...

@Override 
public void onConnected(Bundle arg0) 
{ 
    Toast.makeText(context, "GPS connected", Toast.LENGTH_SHORT).show(); 
    connector = new Connector(); 
    connector.execute("clues", null, null); 
    listener = new LocationListener() 
    { 
     @Override 
     public void onLocationChanged(Location location) 
     { 
      Log.d("location", "listener activated"); 
      try 
      { 
      double[] coords = locationCoords(location); 
      if(coords[0] != 0) 
      { 
       map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(coords[0], coords[1]), (float) 14.5)); 

      } 

      Double latitude = location.getLatitude(); 
      Double longitude = location.getLongitude(); 
      Toast.makeText(context,"Latitude = "+ latitude + "" +"Longitude "+ longitude, 
        Toast.LENGTH_LONG).show(); 
      } 
      catch(NullPointerException e) 
      { 
       Log.d("nulls", "null"); 
      } 

     } 
    }; 
    mLocationClient.requestLocationUpdates(mLocationRequest, listener); 
    Log.d("location", "location updates initiated"); 
+0

我用mMap =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.googlemap))。getMap();並且它似乎刪除了空錯誤,謝謝!一定是試圖讓它變成一個愚蠢的錯誤,就像它是一個普通的片段而不是支持片段。 – fonduman

+0

沒有汗水,你可以通過點擊旁邊的複選標記來接受答案。 – yams

+0

這是否意味着其他人不會回答有關GPS偵聽器的主要問題? – fonduman