2013-03-26 67 views
0

我開發了簡單的應用程序來顯示android的經度和緯度......但它顯示空指針異常...請讓我知道什麼是錯誤..如何在經緯度android

我Activity類

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.Toast; 


public class SignalStrengthActivity extends Activity implements LocationListener{ 

    private LocationManager locationManager; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    } 


    @Override 
    protected void onResume() { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this); 
     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     locationManager.removeUpdates(this); 
     super.onPause(); 
    } 


    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
       Toast.makeText(this, "Latitude and Longitude " + location.getLatitude() + " " + 
         location.getLongitude(), Toast.LENGTH_SHORT).show(); 
      } 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

     Toast.makeText(this, "Latitude and Longitude Offline ", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 


    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 


    } 
} 

我的清單文件: -

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.latitudelongitude" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.latitudelongitude.SignalStrengthActivity" 
      android:configChanges="orientation|keyboardHidden|screenSize" 
      android:label="@string/app_name" 
      android:theme="@style/FullscreenTheme" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

</manifest> 

當我執行它不會顯示任何應用..表明該位置對象爲空...認罪SE讓我知道如何通過下面的鏈接解決問題

+0

你在哪裏執行呢?在模擬器或設備?仿真器不顯示緯度值,在設備中,您需要啓用GPS以測試上述代碼。 – Raynold 2013-03-26 10:38:33

+0

你需要在真實設備上檢查它並確保GPS已打開 – 2013-03-26 10:53:29

+0

Raynold說的是正確的。您應該在啓用GPS的真實設備上進行測試。請注意,GPS可能需要一段時間才能獲得第一次修復。如果你想在仿真器上測試它,那麼你必須發送位置座標給仿真器。檢查此鏈接如何將座標發送到模擬器:http://android-er.blogspot.in/2009/11/change-gps-location-of-android-emulator.html – Nitish 2013-03-26 10:56:23

回答

0

轉到:

Link 1

+0

感謝Manoj ..現在我可以在模擬器中執行它... – 2013-03-26 12:19:04

+0

如果它有助於您接受我的文章。 – 2013-03-26 12:20:52