显示和隐藏活动中的片段

时间:2019-01-15 17:59:54

标签: android android-fragments

我目前正在使用底部导航功能来更改Fragments。但是,我不想破坏并重新创建它们,而是想简单地隐藏并显示片段以保留成员变量。

我尝试了replace()hide()show(),但没有正确解决问题,我遇到了动画错误,无法追踪。

我也找不到在AppCompatActivity中切换片段的示例。

public class MainActivity extends AppCompatActivity {

    PassengerFragment passengerFragment;
    DriverFragment driverFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new PassengerFragment()).commit();
    }

    // this handles the bottom navigation so when you click an item it changes fragment
    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment = null;

                    switch (menuItem.getItemId()) {
                        case R.id.nav_passenger:
                            selectedFragment = passengerFragment;
                            break;

                        case R.id.nav_driver:
                            if (driverFragment==null) {
                                selectedFragment = new DriverFragment();
                            }
                            else {
                                selectedFragment = driverFragment;
                            }

                            break;
                    }

                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                            selectedFragment).commit(); <---- line 54

                    return true;
                }
            };
}

错误

  

Java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.Class java.lang.Object.getClass()'           在android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:396)           在android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:444)           在android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:434)           在je.digital.kevin_pickmeup.MainActivity $ 1.onNavigationItemSelected(MainActivity.java:54)

2 个答案:

答案 0 :(得分:0)

那是因为您尝试用Fragment的{​​{1}}进行替换。您已经将nullpassengerFragment创建为字段或全局变量,并且试图使用它们,但是在某些情况下,特别是driverFragmentpassengerFragment。如果您试图将数据保留在null中,即使它们已被替换,也可以使用Fragments,但是您可能需要通过检查addToBackStack并删除来设法避免将太多数据放回堆栈如果使用getSupportFragmentManager().getBackStackEntryCount()的数目超过1,或者将数据保存在popBackStack()上的Bundle中,然后将数据恢复到onDestroyView中:

答案 1 :(得分:0)

if语句可以更好地处理所有情况。

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Fragment passengerFragment = getSupportFragmentManager().findFragmentByTag("A");
                Fragment driverFragment = getSupportFragmentManager().findFragmentByTag("B");

                switch (menuItem.getItemId()) {
                    case R.id.nav_passenger:
                        if(passengerFragment.isHidden()) {
                            getSupportFragmentManager().beginTransaction().show(passengerFragment).commit();
                            getSupportFragmentManager().beginTransaction().hide(driverFragment).commit();
                        }
                        break;

                    case R.id.nav_driver:
                        if(driverFragment==null) {
                            getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new DriverFragment(),"B").commit();
                            getSupportFragmentManager().beginTransaction().hide(passengerFragment).commit();
                        }
                        else {
                            if(driverFragment.isHidden()) {
                                getSupportFragmentManager().beginTransaction().show(driverFragment).commit();
                                getSupportFragmentManager().beginTransaction().hide(passengerFragment).commit();
                            }
                        }
                        break;
                }
                return true;
            }
};