gpt4 book ai didi

java - fragment 中的 RecyclerView 在设备屏幕旋转时崩溃

转载 作者:行者123 更新时间:2023-11-29 05:02:14 30 4
gpt4 key购买 nike

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

我有一个包含 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) 查看日志输出,似乎我的 Fragment 没有与 Activity 一起被销毁(如果我没记错的话,屏幕旋转会破坏 Activity 并启动一个新 Activity)。

我想找到第 1 点的解决方案,但如果你能给我建议第 2 点的东西就太好了。

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

最佳答案

onActivityCreated() 中的代码移至 onViewCreated() 中。很可能在轮换时,您的 RecyclerViewonActivityCreated() 回调被调用(您设置 LayoutManager 的地方)之前就已布局。

关于java - fragment 中的 RecyclerView 在设备屏幕旋转时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31713157/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com