Fragment中的RecyclerView在设备屏幕旋转时崩溃

时间:2015-07-30 00:00:18

标签: java android android-fragments android-recyclerview

我发现很多问题都不适合我的情况。

我有一个包含Fragment的Activity,里面有一个RecyclerView,带有一个用于管理CardView对象的适配器。

MainActivity.java

private static final String MENU_OPENED = "menu_is_opened";

// For toolbar
private Toolbar toolbar;

// For sliding menu
private DrawerLayout mDrawerLayout;
private ListFragment navMenuFragment;
private ArrayAdapter<String> adapter;
private ArrayList<String> menuList;
private ActionBarDrawerToggle mDrawerToggle;
private boolean menuIsOpened;

//For recyclerview
private RecyclerViewFragment recyclerViewFragment;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d("MAIN_ACTIVITY: ", "onCreate");

    setContentView(R.layout.activity_main);

    menuIsOpened = false;

    initToolbar();

    initDrawer();

    initSlidingMenu();

    initRecyclerView();

    // If an instance of this activity had previously stopped, we can // get the original text it started with.
    if(savedInstanceState!= null)

    {
        // Restore values!
        menuIsOpened = savedInstanceState.getBoolean(MENU_OPENED);
    }


}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    Log.d("MAIN_ACTIVITY: ", "onSaveInstanceState");

    outState.putBoolean(MENU_OPENED, menuIsOpened);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_toolbar, menu); // Inflates action items

    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    switch (item.getItemId())
    {
        case (R.id.searchGroup):
            Log.d("PRESSED Menu Item: ", (String) item.getTitle());
            Toast.makeText(this, "searchGroup selected!", Toast.LENGTH_SHORT).show();
            break;
        case (R.id.addGroup):
            Log.d("PRESSED Menu Item: ", (String) item.getTitle());
            Toast.makeText(this, "addGroup selected!", Toast.LENGTH_SHORT).show();
            break;

        default: return true;
    }

    return true;

}

@Override
public void onClick(View v) // Intercept Navigation Button's click
{
    Toast.makeText(this, "Nav.butt. selected!", Toast.LENGTH_SHORT).show();

}


private void initToolbar()
{
    toolbar = (Toolbar) findViewById(R.id.myToolbar);

    if (toolbar != null)
    {
        setSupportActionBar(toolbar); // set toolbar as the new Action Bar

        toolbar.setNavigationIcon(R.drawable.ic_menu);
        toolbar.setNavigationOnClickListener(this); // Avoid to do "new"

        getSupportActionBar().setDisplayShowTitleEnabled(false);
        // Deletes the activity Label ( don't put it after setTitle! )

        toolbar.setTitle(R.string.toolbarTitle);
    }
}

private void initSlidingMenu()
{
    FragmentManager fm = getFragmentManager();

    navMenuFragment = (ListFragment) fm.findFragmentById(R.id.navigation_menu_fragment);

    if(adapter == null || menuList == null)
    {
        menuList = new ArrayList<String>();
        menuList.add("Map");
        menuList.add("List");
        menuList.add("Profile");

        adapter = new ArrayAdapter<String>(this,
                R.layout.fragment_navigation_drawer,
                menuList);

        navMenuFragment.setListAdapter(adapter);
        navMenuFragment.getView().setBackgroundColor(Color.DKGRAY);
    }
}

private void initDrawer(){

    if(toolbar == null) initToolbar();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            toolbar, R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);

            menuIsOpened = false;
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);

            menuIsOpened = true;
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

private void initRecyclerView(){

    if (recyclerViewFragment == null){
        recyclerViewFragment = new RecyclerViewFragment();
    }

    // Add recyclerViewFragment to Activity
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(R.id.fragments_container, recyclerViewFragment); // calls onInflate() ecc
    ft.commit();



}

您需要关注的方法是initRecyclingView和onCreate()。 然后我有一个 的 RecyclerViewFragment.java

public class RecyclerViewFragment extends Fragment {

private static final String BUNDLE_RECYCLER_VIEW_FRAGMENT_LAYOUT = "bundle.recycler.view.fragment.layout";

private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;


public RecyclerViewFragment(){
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d("RECYCLER_VIEW_FRAGM: ", "onCreateView");

    return inflater.inflate(R.layout.fragment_recycler, container, false);
}

/* From Docs: Called after Activity.OnCreate has been completed by the hosting Activity.
 * Final tweaks to the user interface should be performed at this time.
 */
@Override
public void onActivityCreated(Bundle savedInstanceState) {

    Log.d("RECYCLER_VIEW_FRAGM: ", "onActivityCreated");

    super.onActivityCreated(savedInstanceState);

    // I know that the recyclerView has been instantiated ( look at onCreateView() )
    mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.my_recycler_view);

    /* use this setting to improve performance if you know that changes
     * in content do not change the layout size of the RecyclerView
     */
    //mRecyclerView.setHasFixedSize(true);

    // using a linear layout manager as doc suggest
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);


    ArrayList<Group> al = new ArrayList<>();
    Group g = new Group("My First Group", R.drawable.ic_group_icon);
    al.add(g);
    al.add(g);
    al.add(g);
    al.add(g);
    al.add(g);
    al.add(g);

    // specify an adapter
    Adapter recyclerViewAdapter = new RecyclerViewAdapter(al);
    mRecyclerView.setAdapter(recyclerViewAdapter);

}

最后我遇到了两个问题:

1)当我旋转屏幕时,我得到以下异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
        at android.view.View.measure(View.java:17547)

2)看着Log输出,似乎我的Fragment没有与Activity一起被销毁(如果我没有错,屏幕的旋转会破坏活动并开始新的活动)。

我想找到第1点的解决方案,但是如果你可以建议我2的东西会很棒。

感谢。 (请询问您是否需要更多代码,例如xml文件)

3 个答案:

答案 0 :(得分:3)

onActivityCreated()中的代码移至onViewCreated()。轮换时,您的RecyclerView可能会在调用onActivityCreated()回调之前进行布局(您设置LayoutManager的位置)。

答案 1 :(得分:0)

// I know that the recyclerView has been instantiated ( look at onCreateView() )
mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.my_recycler_view);

你绝对应该在Fragment中使用它。要获取片段中的视图,请将其放在onCreateView或onViewCreated中。你有片段的根视图实例,然后使用View childView = rootView.findViewById(someId)代替。

答案 2 :(得分:0)

我很惊讶您只会在方向更改时遇到此错误。您的RecyclerView需要正确设置LayoutManager才能正常使用。问题是您在RecyclerView中创建Activity,但是在Fragment中初始化它,并且通常会异步添加或初始化片段,这在某些情况下意味着{{1}将尝试在Activity执行RecyclerView之前的初始化代码之前显示Fragment

RecyclerView及其初始化代码都应位于同一容器中:ActivityFragment。避免从Fragment的{​​{1}}或Activity视图访问Activity次观看。