旋转设备时,RecyclerView / MapView崩溃

时间:2016-03-21 12:38:02

标签: android google-maps xamarin android-recyclerview android-mapview

我的Android.Support.V4.Fragment包含MapViewRecyclerView。这些是Fragment中的单独视图。

设备旋转时应用程序崩溃:

  

Android.OS.BadParcelableException:解组时的ClassNotFoundException:android.support.v7.widget.RecyclerView $ SavedState

我按照文档的要求将生命周期方法传递给MapView:

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)崩溃,为什么会这样?

2 个答案:

答案 0 :(得分:5)

对于任何搜索并遇到同样问题的人。 This is the solution,它似乎是一个2岁的小虫。

  

我能够通过将MapView的保存状态保存在单独的Bundle上然后将其添加到传出的已保存状态包中来解决它。然后在onCreate()中,我只是从传入的状态中获取MapView的保存状态,并将其传递给onCreate()方法,从而停止崩溃。

在我的情况下,我实施如下:

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); 
}

答案 1 :(得分:0)

Tallpaul的回答并不适合我。他联系的解决方案虽然运作良好。

 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
}