旋转设备时,RecyclerView / MapView崩溃 [英] RecyclerView / MapView crashes when rotating device
问题描述
我有一个 Android.Support.V4.Fragment
,它包含一个 MapView
和一个 RecyclerView
。这些是 Fragment
中的单独视图。
I have a Android.Support.V4.Fragment
that contains both a MapView
and a RecyclerView
. These are separate views in the Fragment
.
设备旋转时,应用程序崩溃:
The app crashes when the device is rotated:
Android.OS.BadParcelableException:解组时发生ClassNotFoundException:android.support.v7.widget.RecyclerView $ SavedState
Android.OS.BadParcelableException: ClassNotFoundException when unmarshalling: android.support.v7.widget.RecyclerView$SavedState
我将生命周期方法按照文档的要求传递给MapView:
I am passing the lifecycle methods to the MapView as required by the docs:
private MapView mapView;
private RecyclerView myRecyclerView;
private RecyclerView.LayoutManager myLayoutManager;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
myRecyclerView = myRootView.FindViewById<RecyclerView>(Resource.Id.myRecyclerView);
myLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.Horizontal, false);
myRecyclerView.SetLayoutManager(myLayoutManager);
myRecyclerAdapter = ...
myRecyclerView.SetAdapter(myRecyclerAdapter);
mapView.OnCreate(savedInstanceState);
}
public override void OnSaveInstanceState(Bundle outState)
{
...
if (mapView != null) mapView.OnSaveInstanceState(outState);
base.OnSaveInstanceState(outState);
}
如果我删除 RecyclerView
形成它正确旋转的AXML,如果包含它,应用程序崩溃在 mapView.OnCreate(savedInstanceState)
这是为什么?
If I remove the RecyclerView
form the AXML it rotates correctly, if I include it the app crashes at mapView.OnCreate(savedInstanceState)
why is this?
推荐答案
对于任何搜索并具有相同问题的人。 这是解决方案,它会似乎是2年前的一个bug。
For anyone that searches and has the same problem. This is the solution, It would appear to be a 2yr old bug.
我可以通过将MapView的保存状态保存在单独的Bundle上,然后添加它到传出的保存状态包。然后,在
onCreate()
中,我只需从传入的地图中获取MapView的保存状态并将其传递给它的onCreate()
方法,从而停止崩溃。
I was able to get around it by saving the MapView's saved state on a separate Bundle and then adding it to the outgoing saved state bundle. Then in
onCreate()
, I would just grab the MapView's saved state from the incoming one and pass it into itsonCreate()
method, thus stopping the crashes.
在我的情况下,我的实现如下:
In my case I implemented as so:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
myRecyclerView = myRootView.FindViewById<RecyclerView>(Resource.Id.myRecyclerView);
myLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.Horizontal, false);
myRecyclerView.SetLayoutManager(myLayoutManager);
myRecyclerAdapter = ...
myRecyclerView.SetAdapter(myRecyclerAdapter);
Bundle mapViewSavedInstanceState = savedInstanceState != null ? savedInstanceState.GetBundle("mapViewSaveState") : null;
mapView.OnCreate(mapViewSavedInstanceState);
}
public override void OnSaveInstanceState(Bundle outState)
{
if (mapView != null)
{
Bundle mapViewSaveState = new Bundle(outState);
outState.PutBundle("mapViewSaveState", mapViewSaveState);
mapView.OnSaveInstanceState(outState);
}
base.OnSaveInstanceState(outState);
}
这篇关于旋转设备时,RecyclerView / MapView崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!