我有一个抽屉布局,它在两个片段之间导航(就像它在创建一个带有抽屉布局的新项目时设置的那样:所以有一个导航图和 AppBarConfiguration 的一般设置。我刚刚删除了一大堆东西,所以我最终只会有两个片段)。
问题:当我导航到第二个片段并更改设备的方向两次(因此翻转手机横向,然后返回纵向)然后我在起始目的地的 onSavedInstanceState() 方法中得到一个 nullpointerException。我想知道为什么会这样?有没有办法弹出那个片段或其他东西,因为它甚至不可见?我有点期待导航 UI 能够正确处理这个问题,因为我认为这是它的目的 - 但是,现在我觉得我可能错过了一些真正重要的东西
导航图:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="SomeNameHome"
android:label="@string/menu_auction"
tools:layout="@layout/fragment_auction" />
<fragment
android:id="@+id/nav_gallery"
android:name="SomeNameGallery"
android:label="@string/menu_basket"
tools:layout="@layout/fragment_basket" />
</navigation>
然后,家庭活动:
/** Activity containing the Auction and Basket fragments. */
public class HomeActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery)
.setDrawerLayout(drawer)
.build();
navigationView.setItemIconTintList(null);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
问题代码,在起始目标片段中:
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
LinearLayoutManager m = (LinearLayoutManager) mSearchResultsList.getLayoutManager();
outState.putInt(SAVED_INSTANCE_POSITION, m.findFirstCompletelyVisibleItemPosition() );
}
mSearchResultList 在第二次方向更改时为空。