Android 更改配置时,MapView在具有协调器布局的片段中崩溃
我的自定义片段在Android 更改配置时,MapView在具有协调器布局的片段中崩溃,android,android-layout,android-view,android-mapview,android-coordinatorlayout,Android,Android Layout,Android View,Android Mapview,Android Coordinatorlayout,我的自定义片段在MapView和CoordinatorLayout中有问题。我做了一个简单的应用来说明这一点。我也尝试过在标准的GoogleAPI演示上做同样的事情,但没有出现任何问题,但这可能是因为他们使用的是MapFragment,而不是MapView 下面,我上传了代码和布局。最后还粘贴了一条错误跟踪 public class BlankFragment extends Fragment implements OnMapReadyCallback { private MapVie
MapView
和CoordinatorLayout
中有问题。我做了一个简单的应用来说明这一点。我也尝试过在标准的GoogleAPI演示上做同样的事情,但没有出现任何问题,但这可能是因为他们使用的是MapFragment
,而不是MapView
下面,我上传了代码和布局。最后还粘贴了一条错误跟踪
public class BlankFragment extends Fragment implements OnMapReadyCallback {
private MapView mapView;
private Bundle BundleForMap;
@Override
public void onMapReady(GoogleMap googleMap) { }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BundleForMap = savedInstanceState;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank, container, false);
mapView = (MapView) view.findViewById(R.id.map_event_mapview);
mapView.onCreate(BundleForMap);
mapView.getMapAsync(this);
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
mapView.onDestroy();
super.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
mapView.onSaveInstanceState(savedInstanceState);
super.onSaveInstanceState(savedInstanceState);
}
}
活动只需调用该片段并用其替换容器视图:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment blankFragment = new BlankFragment();
fragmentTransaction.replace(R.id.container, blankFragment, "");
fragmentTransaction.commit();
}
下面是片段布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mapview_test.BlankFragment">
<com.google.android.gms.maps.MapView
android:id="@+id/map_event_mapview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/map_event_bottom_sheet"
android:layout_height="match_parent"
android:layout_width="match_parent" >
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="175dp"
android:elevation="16dp"
android:outlineProvider="bounds"
android:background="@android:color/white"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textSize="16sp"
android:id="@+id/testText"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
这一行的问题在于:
mapView.onCreate(BundleForMap);
当屏幕配置更改时,此参数显然不是null
,应用程序崩溃。在这里传递null
而不是bundle时,它可以正常工作。当我删除协调器布局时,应用程序工作正常
以下是我的依赖项:
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.example.mapview_test"
minSdkVersion 22
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:support-v4:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
compile 'com.google.android.gms:play-services-appindexing:9.4.0'
compile 'com.google.android.gms:play-services-maps:9.4.0'
compile 'com.google.android.gms:play-services-location:9.4.0'
}
我被这个问题困扰了很久。任何帮助都将不胜感激这似乎是众所周知的问题: 我已根据以下答案实施了自己的解决方案: 具体实施如下(基于
#C9
答案):
我可以通过将MapView的已保存状态保存在单独的捆绑包中,然后将其添加到传出的已保存状态捆绑包中来绕过它。然后在onCreate()中,我将从传入的MapView中获取MapView的保存状态,并将其传递到其onCreate()方法中,从而停止崩溃
然后,在onCreate
中使用mapViewSaveState
(如果是片段,则使用onCreateView
):
我的
活动
与包含框架布局
的抽屉布局
存在相同的问题,而我的片段
包含地图视图
只是一个框架布局
。。。这似乎已经解决了它,所以谢谢你!我不知道你怎么知道的!
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.example.mapview_test"
minSdkVersion 22
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:support-v4:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
compile 'com.google.android.gms:play-services-appindexing:9.4.0'
compile 'com.google.android.gms:play-services-maps:9.4.0'
compile 'com.google.android.gms:play-services-location:9.4.0'
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
final Bundle mapViewSaveState = new Bundle(savedInstanceState);
mapView.onSaveInstanceState(mapViewSaveState);
savedInstanceState.putBundle("mapViewSaveState", mapViewSaveState);
Bundle customBundle = new Bundle();
// put custom objects if needed to customBundle
savedInstanceState.putBundle(ARG_CUSTOM_BUNDLE, customBundle);
super.onSaveInstanceState(savedInstanceState);
}
mapView.onCreate(savedInstanceState != null ? savedInstanceState.getBundle("mapViewSaveState") : null);