2012-02-23 74 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密钥;)