initCameraメソッドでlocation.getLatitudeを呼び出すときにnullポインタ例外が発生します。私はスタック上のすべてのソリューションを試したが、誰も働いていない。Google maps V2位置がnullを返す
Javaコードを
public class MapFragment extends SupportMapFragment implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
GoogleMap.OnInfoWindowClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.OnMapClickListener,
GoogleMap.OnMarkerClickListener {
private GoogleApiClient mGoogleApiClient;
private Location mCurrentLocation;
private LocationRequest mLocationRequest;
private final int[] MAP_TYPES = { GoogleMap.MAP_TYPE_SATELLITE,
GoogleMap.MAP_TYPE_NORMAL,
GoogleMap.MAP_TYPE_HYBRID,
GoogleMap.MAP_TYPE_TERRAIN,
GoogleMap.MAP_TYPE_NONE };
private int curMapTypeIndex = 0;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
initListeners();
}
@Override
public void onConnected(Bundle bundle) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Log.d("mCurrentLocation",mCurrentLocation +"");
initCamera(mCurrentLocation);
}
@Override
public void onDestroy() {
super.onDestroy();
removeListeners();
}
private void initListeners() {
getMap().setOnMarkerClickListener(this);
getMap().setOnMapLongClickListener(this);
getMap().setOnInfoWindowClickListener(this);
getMap().setOnMapClickListener(this);
}
private void removeListeners() {
if(getMap() != null) {
getMap().setOnMarkerClickListener(null);
getMap().setOnMapLongClickListener(null);
getMap().setOnInfoWindowClickListener(null);
getMap().setOnMapClickListener(null);
}
}
private void initCamera(Location location) {
if (location != null){
CameraPosition position = CameraPosition.builder()
.target(new LatLng(location.getLatitude(), location.getLongitude()))
.zoom(16f)
.bearing(0.0f)
.tilt(0.0f)
.build();
getMap().animateCamera(CameraUpdateFactory.newCameraPosition(position), null);
getMap().setMapType(MAP_TYPES[curMapTypeIndex]);
getMap().setTrafficEnabled(true);
getMap().setMyLocationEnabled(true);
getMap().getUiSettings().setZoomControlsEnabled(true);
} else {
Log.d("hello","hello");
}
}
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
if(mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionSuspended(int i) {
//handle play services disconnecting if location is being constantly used
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
//Create a default location if the Google API Client fails. Placing location at Googleplex
mCurrentLocation = new Location("");
mCurrentLocation.setLatitude(37.422535);
mCurrentLocation.setLongitude(-122.084804);
initCamera(mCurrentLocation);
}
}
マニフェスト許可
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
依存
:以下は、マニフェストとの依存関係私が使用している、私のフラグメントのコードです
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.google.android.gms:play-services-maps:7.8.0'
compile 'com.google.android.gms:play-services-location:7.8.0'
}
https://stackoverflow.com/questions/25017764/play-location-services-getlastlocation-returns-null – AndroidHacker
このリンクには解決策がありません –
私は完全な解決策を読んでいないと思います。 – AndroidHacker