2013-06-04 49 views
1

我試圖跟蹤GPS位置在我的應用程序中使用,位置管理器顯示空

這裏是我的代碼,

public class GetGpsCoordinates extends Service implements LocationListener { 

boolean isGPSEnabled ; 

boolean isNetworkEnabled ; 
boolean canGetLocation ; 
Location location; 
double latitude; 
double longitude; 

// The minimum distance to change Updates in meters 
public static final float MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 
                    // meters 

// The minimum time between updates in milliseconds 
public static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

private final Context Context = GetGpsCoordinates.this; 
public final android.content.Context mContext = GetGpsCoordinates.this; 
LocationManager locationManager; 

public Location getLocation() { 
    try { 
     int chkGooglePlayServices = GooglePlayServicesUtil 
       .isGooglePlayServicesAvailable(Context); 
     if (chkGooglePlayServices != ConnectionResult.SUCCESS) { 
      GooglePlayServicesUtil.getErrorDialog(chkGooglePlayServices, 
        (Activity) mContext, 1122).show(); 
     } else { 

      locationManager = (LocationManager) getSystemService(mContext.LOCATION_SERVICE); 
      Criteria criteria = new Criteria(); 
      criteria.setAccuracy(Criteria.ACCURACY_FINE); 

      // getting GPS status 
      isGPSEnabled = locationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      isNetworkEnabled = locationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!isGPSEnabled && !isNetworkEnabled) { 
       // no network provider is enabled 
      } else { 
       this.canGetLocation = true; 
       // First get location from Network Provider 
       if (isNetworkEnabled) { 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 

          } 
         } 
        } 
       } 
      } 

     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return location; 
} 

我不知道我錯過了什麼,變量「 locationManager「顯示爲空。

這是我的清單文件,

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.bu" 
android:versionCode="2" 
android:versionName="2.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="com.bu.permission.MAPS_RECEIVE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 


<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:theme="@style/NoTitleBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".HomeScreen" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.Light"> 
    </activity> 

    <activity 
     android:name=".PropertySearchTypes.CameraSearch" 
     android:label="@string/camera_search" 
     android:screenOrientation="unspecified"/> 
    <activity 
     android:name=".PropertySearchTypes.MapSearch" 
     android:label="@string/map_search" /> 
    <service 
     android:name=".PropertySearchTypes.PropertyRequestService" > 

    </service> 
    <meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="API_KEY" /> 
</application> 

</manifest> 

這是我的logcat,

06-04 19:21:08.960: D/BatteryService(1499): update start 
06-04 19:21:10.101: D/dalvikvm(16760): GC_EXPLICIT freed 17K, 49% free 2778K/5379K, external 688K/1036K, paused 54ms 
06-04 19:21:10.210: E/StatusBarPolicy(1552): ecio: 255 
06-04 19:21:10.210: E/StatusBarPolicy(1552): iconLevel: 4 
06-04 19:21:12.109: D/dalvikvm(18597): JDWP invocation returning with exceptObj=0x40614540 (Ljava/lang/NullPointerException;) 
06-04 19:21:12.906: D/dalvikvm(18597): JDWP invocation returning with exceptObj=0x406146a0 (Ljava/lang/NullPointerException;) 
06-04 19:21:15.265: D/dalvikvm(15818): GC_EXPLICIT freed 481K, 49% free 3529K/6791K, external 688K/1036K, paused 53ms 
06-04 19:21:18.992: D/BatteryService(1499): update start 
06-04 19:21:20.265: D/dalvikvm(14873): GC_EXPLICIT freed 199K, 47% free 3590K/6727K, external 688K/1036K, paused 50ms 

調用另一個動作,

protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.map_fragment); 
    progressDialog = new ProgressDialog(MapSearch.this); 

      /* getting latitude and longitude*/ 
    GetGpsCoordinates getgpslatlng = new GetGpsCoordinates(); 
    getgpslatlng.getLocation(); 

    Latitude = getgpslatlng.getLatitude(); 
    Longitude = getgpslatlng.getLongitude(); 
    SupportMapFragment sfm = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    map = sfm.getMap(); 
    map.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
    map.setMyLocationEnabled(true); 
} 

我添加了所有日epermissions。我啓用了我的設備中的GPS。即使引發了一個異常,指出「locationManager」null和「chkGooglePlayServices」爲空。我可以在我的設備中安裝Google播放服務。

什麼是錯在我的代碼?請糾正我.. 在此先感謝!

+1

首先要確定什麼是你正在使用正確的代碼,而不是在問題更改代碼,並添加記錄與問題,從獲得更多的幫助 –

+0

你在哪裏打電話的getLocation? – nandeesh

+0

@nandeesh,從另一個活動調用getLocation().. – Mahe

回答

2

你正在從活動初始化服務。這不是初始化服務的正確方法。服務需要由Frameworks初始化。 所以你的Service類沒有初始化上下文。所以任何對上下文方法的調用都會失敗。 那麼試試這個

GetGpsCoordinates getgpslatlng = new GetGpsCoordinates(); 
    getgpslatlng.getLocation(this); 

public Location getLocation(Context mContext) { 
//use passed Context instead of field mContext 
} 
+0

明白了!謝謝!! – Mahe

0

你沒有初始化的LocationManager,請使用

的LocationManager =(的LocationManager)context.getSystemService(LOCATION_SERVICE);

使用的LocationManager

2

這是一種錯誤的方式初始化的LocationManager之前,

locationManager = (LocationManager) getSystemService(mContext.LOCATION_SERVICE); 

相反mContext的,你需要使用下面直接Context類,

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
+0

啊!如你所說修改了代碼,同樣的問題。請查看logcat瞭解異常的原因。 – Mahe