我第一次进入地图活动时,它会正确显示我想要的所有内容 - 我的位置,工具和地图放大到我的位置:
然后,当我离开这个活动,然后回来时,我看到了:
这是我的活动代码: @覆盖 public void onStart(){ super.onStart(); }
@Override
public void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(RESULT_CANCELED, intent);
finish();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_my_location);
//...
Log.d(TAG, "Get user's location in a variable");
userLocation = new LatLng(MainActivity.CURRENT_LATITUDE, MainActivity.CURRENT_LONGITUDE);
Log.d(TAG, "Set user's location to "+MainActivity.CURRENT_LATITUDE+", "+MainActivity.CURRENT_LONGITUDE);
// I've tested for this to be 0.0 or null or something - it's perfectly captured every time.
//...
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (googleMap == null) {
// Try to obtain the map from the SupportMapFragment.
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (googleMap != null) {
setUpMap();
}
}else{
setUpMap();
}
}
private void setUpMap() {
try {
// Loading map
googleMap.setOnCameraChangeListener(this);
googleMap.setOnMapClickListener(this);
Log.d(TAG, "Changing map type");
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
Log.d(TAG, "Showing / hiding your current location");
googleMap.setMyLocationEnabled(true);
Log.d(TAG, "Enable / Disable zooming controls");
googleMap.getUiSettings().setZoomControlsEnabled(false);
Log.d(TAG, "Enable / Disable my location button");
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
Log.d(TAG, "Enable / Disable Compass icon");
googleMap.getUiSettings().setCompassEnabled(true);
Log.d(TAG, "Enable / Disable Rotate gesture");
googleMap.getUiSettings().setRotateGesturesEnabled(true);
Log.d(TAG, "Enable / Disable zooming functionality");
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setTiltGesturesEnabled(true);
googleMap.setPadding(0, 0, 0, (int) getResources().getDimension(R.dimen.height_button_dialog));
Log.d(TAG, "Move map camera to user's location");
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 13));
} catch (Exception e) {
e.printStackTrace();
}
}
为什么它第一次完美运行,但不是第二次(以及之后每次)?
我记录了所有内容 - 没有抛出任何异常或零或空的内容。
这是我的清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_LOGS"/>
<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" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.GET_TASKS" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!-- Service wakelock -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="my.package.name.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="my.package.name.permission.C2D_MESSAGE" />
<!-- ...App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Deprecated
http://stackoverflow.com/a/17937382/956975
This permission is now completely unnecessary. The latest update of Google Play Services 3.1.59 made it useless. As a result, it can be removed.
<permission
android:name="my.package.name.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="my.package.name.permission.MAPS_RECEIVE" />-->
<application
tools:replace="android:icon"
android:name="@string/app_name"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true"
android:installLocation="preferExternal"
>
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Goolge Maps API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my_API_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<uses-library
android:name="com.google.android.maps"
android:required="false" />
</application>
我的Gradle文件:
buildscript {
repositories {
maven { url 'http://download.crashlytics.com/maven' }
}
dependencies {
classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'crashlytics'
repositories {
maven { url 'http://download.crashlytics.com/maven' }
}
android {
compileSdkVersion 'Google Inc.:Google APIs:14'
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "my.package.name"
minSdkVersion 14
targetSdkVersion 21
versionCode 46
versionName "2.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
packagingOptions{
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
}
dependencies {
compile project(':countryPicker')
compile files('libs/apache-mime4j-core-0.7.2.jar')
compile 'org.apache.commons:commons-lang3:3.3.2'
compile files('libs/FlurryAnalytics_3.3.1.jar')
compile files('libs/httpmime-4.1.2.jar')
compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.4'
compile 'com.fasterxml.jackson.core:jackson-core:2.4.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.4'
compile 'com.googlecode.libphonenumber:libphonenumber:7.0.1'
compile files('libs/log-1.0.4.jar')
compile files('libs/universal-image-loader-1.8.4-with-sources.jar')
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.crashlytics.android:crashlytics:1.+'
//https://github.com/square/picasso
compile 'com.squareup.picasso:picasso:2.3.2'
/*For animation ease*/
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.daimajia.easing:library:1.0.0@aar'
}
请帮帮忙?
答案 0 :(得分:0)
我已经解决了这个问题,但我不确定它是不是最好的方法。
在onPause(),onDestroy(),onBackPressed()和我的活动开始时(我声明我的类变量),我设置了googleMap = null。
这样,googleMap始终为null,并且必须始终重新实例化。
现在,我的地图每次都可以完美地加载我的位置和所有其他内容。
我打算提出如何更好地做这件事的建议,因为我觉得这有点像hacky ......