2012-02-23 122 views
1

當我在模擬器上運行應用程序時,但是當我在手機上運行應用程序時(三星mini),我的應用程序崩潰(我不知道爲什麼,我做了打開GPS和互聯網)。使用GPS無法獲取當前位置(應用程序崩潰)

代碼:

public class atm_atmogan extends MapActivity { 
    LocationManager mlocManager; 
    LocationListener mlocListener; 
    MapController mapController; 
    MapView mapView; 
    Location location; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.atm_atmogan); 

     mapView = (MapView) findViewById(R.id.map_view); 
     mapView.setBuiltInZoomControls(true); 
     mapView.setTraffic(true); 
     mapView.invalidate(); 
     mapController = mapView.getController(); 
     mapController.setZoom(17); 

     mlocManager = (LocationManager) this 
       .getSystemService(Context.LOCATION_SERVICE); 
     location = mlocManager 
       .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
       mlocListener); 
     mapController.animateTo(convertLoc(location)); 
    } 

    public class MyLocationListener implements LocationListener { 
     @Override 
     public void onLocationChanged(Location loc) { 
      location = loc; 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      Toast.makeText(getApplicationContext(), "GPS OFF", 
        Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

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

convertLoc是功能我創建轉換位置的GeoPoint

更新logcat的時候我在真機上運行:

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:130) 
at android.app.ActivityThread.main(ActivityThread.java:3687) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
at tim_atm.namespace.atm_atmogan.convertLoc(atm_atmogan.java:115) 
at tim_atm.namespace.atm_atmogan.onCreate(atm_atmogan.java:146) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
... 11 more 

更新佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <com.google.android.maps.MapView 
     android:id="@+id/map_view" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:clickable="true" 
     android:apiKey="0rMwFsILEdg4813dweucAH5iRKJHGozQs8FFvzQ" 

     /> 
</RelativeLayout> 
+1

把你的錯誤在你的問題...... – 2012-02-23 15:44:26

+1

什麼是logcat的輸出應用程序崩潰的時候? – bschultz 2012-02-23 15:44:30

+0

當我在模擬器上運行應用程序...一切正常..沒有錯誤。應用程序崩潰時,我在我的真實手機上運行應用程序(Samsung galaxy mini).. – HoangQBH 2012-02-23 15:46:49

回答

0

請檢查mlocManager.getLastKnownLocation()返回一個非空值。

+0

我不知道爲什麼方法getLastKnownLocation()返回空值...我在真實設備上測試你能告訴我如何解決我的問題 – HoangQBH 2012-02-23 17:12:06

+0

啓用你的GPS和如果位置爲空並且調用'mapController.animateTo(convertLoc(location));',那麼設備可以訪問gps衛星並存儲一個位置,同時調用'mapController.animateTo(convertLoc(location)); (位置));'從onLocationChanged()方法的監聽器。 – 2012-02-23 17:16:32

+0

我已經啓用GPS,互聯網,使用無線網絡,使用GPS衛星:( – HoangQBH 2012-02-23 17:19:57

0

GetLastKnownLocation不保證你會得到位置。您應該聽取位置更改,然後將該位置用作備份計劃。您可能想使用其他參數嘗試getLastKnownLocation。

此外,您可能需要刪除您的地圖API密鑰;)