2016-02-18 60 views
-2

我已經創建了FiltersQueryAdapter作爲mainAdapter,它已被傳遞給setupPhotoFilters方法,其中mainadapter被配置爲setAdapter。有可能是因爲parseGeoPointfromLocation不在FiltersQueryApdapter的範圍內,它返回null我在這種情況下做了什麼?NullPointException double getLatitude()

/* 
    * Helper method to get the Parse GEO point representation of a location 
    */ 
    public ParseGeoPoint geoPointFromLocation(Location loc) { 
     return new ParseGeoPoint(loc.getLatitude(), loc.getLongitude()); 
    } 


    FiltersQueryAdapter mainAdapter = new FiltersQueryAdapter(this, PhotoFiltersAdapter.class 
      , new ParseRecyclerQueryAdapter.QueryFactory() { 
     public ParseQuery create() { 
      Location myLoc = (currentLocation == null) ? lastLocation : currentLocation; 
      ParseQuery query = ParseQuery.getQuery("PlaceFilters"); 
      //query.include("user"); 
      query.orderByAscending("GeoArea"); 
      query.whereWithinKilometers("GeoArea", geoPointFromLocation(myLoc), radius); 
      query.setLimit(6); 
      return query; 
     } 
    }); 


    private void setupPhotoFilters() { 
     rvFilters.setHasFixedSize(true); 
     rvFilters.setAdapter(mainAdapter); 
     rvFilters.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); 
    } 
+0

in GeoLocationHelper? –

+0

閱讀錯誤消息。問題出現在TakePhotoActivity.java的第174行。可能是由於GeoLocationHelper沒有將currentBestLocation設置爲任何值而引起的。 – FredK

+0

似乎如果訪問Google Play位置服務的參數設置不正確,就會發生問題。我遵循[本教程](http://developer.android.com/training/location/index.html)在應用程序上設置位置服務。爲我完美工作。讓我知道事情的後續。 –

回答

1

你正在做的假設,該行總是返回一個非空值:

currentBestLocation = locationManager 
    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

javadoc表明,它可以爲空,所以你應該經常檢查它的使用它。您通過getCurrentLocation()傳播該值,請在此處查看。