android当前位置导致应用崩溃
我创建了一个概念应用,抓取三个位置并在Google地图上显示它们:我的学校,我的家和我的当前位置。android当前位置导致应用崩溃
该应用程序曾经工作,但现在它每当我要求它拉当前位置时崩溃。上周我没有对该应用进行任何更改,但现在它无法正常工作。奇怪的是,它仍然会拉我的学校和家庭。
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.location.Location;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
//Loosely based on an App tutorial from Youtube by ram goud.
public class MapActivity extends Activity implements android.location.LocationListener {
private final LatLng LOCATION_HOME = new LatLng(45.310488, -93.536989);
private final LatLng LOCATION_SCHOOL = new LatLng(44.972586, -93.283689);
protected LocationManager locationManager;
protected LocationListener locationListener;
protected boolean gps_enabled, network_enabled;
Button homeButton;
Button schoolButton;
Button currentLocation;
private double lat;
private double lng;
private final LatLng LOCATION_CURRENT = null;
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
map =((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
map.getUiSettings().setRotateGesturesEnabled(false); //I dislike this feature. That is why it has been removed.
// Criteria criteria = new Criteria();
// String provider = locationManager.getBestProvider(criteria, gps_enabled);
// final Location myLocation = locationManager.getLastKnownLocation(provider);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
homeButton = (Button)findViewById(R.id.home_button);
homeButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN); //MAP_TYPE_TERRAIN sets the map displayed as just a normal Google Map image.
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_HOME, 14); // newLatLngZoom is for zooming. the # represents the distance. 21 furthest & 1 being closest.
map.animateCamera(update);
map.addMarker(new MarkerOptions().position(LOCATION_HOME).title("Hello, Home"));
}
});
schoolButton = (Button)findViewById(R.id.school_button);
schoolButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE); //MAP_TYPE_SATELLITE sets the map displayed as a sat image
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_SCHOOL, 16); // newLatLngZoom is for zooming. the # represents the distance. 21 furthest & 1 being closest.
map.addMarker(new MarkerOptions().position(LOCATION_SCHOOL).title("Hello, School"));
map.animateCamera(update);
}
});
currentLocation = (Button)findViewById(R.id.current);
currentLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GoToCurrent();
}
});
}
private void GoToCurrent() { //Method created to find current lat/long of user.
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, gps_enabled);
Location myLocation = locationManager.getLastKnownLocation(provider);
double lati = myLocation.getLatitude();
double longi = myLocation.getLongitude();
LatLng latlng = new LatLng(lati, longi);
map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
map.animateCamera(CameraUpdateFactory.zoomTo(20));
map.addMarker(new MarkerOptions().position(new LatLng(lati, longi)).title("You are here"));
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
//这是清单,以防万一有人好奇。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="com.google.android.gms.version"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- The following two permissions are not required to use Google Maps Android API v2, but are recommended. -->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MapActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxxxx-xxxxxxxx" /> <!-- API key -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
</application>
</manifest>
你不需要调用它的任何方法
Location myLocation = locationManager.getLastKnownLocation(provider);
,如果你看一下文档,发现前检查,如果你的价值myLocation变量为空或here
你会看到这个函数可以返回null
public Location getLastKnownLocation(String提供者)在API中添加 级别1
返回一个位置,指示从给定提供者获取的最后一个已知位置 修复的数据。
这可以在不启动提供程序的情况下完成。请注意,此 位置可能已过期,例如,如果该设备已关闭并移动到其他位置,则会关闭该位置 。
如果提供者当前被禁用,则返回null。参数 提供商提供者的名称返回
the last known location for the provider, or **null**
抛出SecurityException若没有合适的权限是目前 IllegalArgumentException - 如果提供者为null或不存在
那么奇怪的是,它的工作没有。代码没有改变。它只是...的作品!
它的工作原因,是因为getLastKnownLocation(provider)有时只返回null。如果你看看上面的文档,你会发现,该方法__can__返回null。如果你忽略这个事实,那么在某个时候,应用程序__将会像之前那样崩溃。很容易就放弃似乎已经改正它的东西,但是如果你想学习某些东西,那么检查null条件并显示一个toast,或者将某些东西打印到日志或任何东西,如果它为null和__don '调用空对象上的任何方法。最糟糕的情况下,你永远不会看到吐司/日志消息! – nPn 2014-12-12 03:22:40
您可以添加堆栈跟踪吗? – 2014-12-10 21:38:47