当我在模拟器上运行应用程序时,一切正常,但是当我在手机上运行应用程序(三星galaxy mini)时,我的应用程序崩溃(我不知道为什么,我确实打开了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>
答案 0 :(得分:0)
请检查mlocManager.getLastKnownLocation()
是否返回非空值。
答案 1 :(得分:0)
GetLastKnownLocation不保证您将获得位置。您应该监听位置更改,然后将该位置用作备份计划。您可能希望使用其他参数尝试getLastKnownLocation。
此外,您可能希望删除Map API密钥;)