从一个片段传递到另一个片段

时间:2016-02-29 09:06:55

标签: java android android-layout android-fragments android-studio

我正在尝试创建一个菜单,在点击时从一个片段传递到另一个片段。当我按下菜单中的特定项目(活动)时,活动退出,故障

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();// this line has the problem

这是我完整的logcat:

FATAL EXCEPTION: main                                                                               java.lang.NullPointerException                                        
     

在   android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:414)   在   android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:449)   在   android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:441)   atbr.exemplozxingintegration.MainActivity.selectDrawerItem(MainActivity.java:84   atbr.exemplozxingintegration.MainActivity $ 1.onNavigationItemSelected(MainActivity.java:50)   在   android.support.design.widget.NavigationView $ 1.onMenuItemSelected(NavigationView.java:150)                                                                                           在   android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)                                                                                           在   android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)                                                                                           在   android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)                                                                                           在   android.support.design.internal.NavigationMenuPresenter $ 1.onClick(NavigationMenuPresenter.java:308)                                                                                           在android.view.View.performClick(View.java:4439)                                                                                           在android.view.View $ PerformClick.run(View.java:18395)                                                                                           在android.os.Handler.handleCallback(Handler.java:725)                                                                                           在android.os.Handler.dispatchMessage(Handler.java:92)                                                                                           在android.os.Looper.loop(Looper.java:176)                                                                                           在android.app.ActivityThread.main(ActivityThread.java:5319)                                                                                           at java.lang.reflect.Method.invokeNative(Native Method)                                                                                           在java.lang.reflect.Method.invoke(Method.java:511)                                                                                           在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)                                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)                                                                                           在dalvik.system.NativeStart.main(本地方法)

MainActivity.java

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        txResult = (TextView) findViewById(R.id.txResult);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        nvDrawer = (NavigationView) findViewById (R.id.nvView);
        setupDrawerContent(nvDrawer);}



    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        selectDrawerItem(menuItem);
                        return true;
                    }
                });
    }

    public void selectDrawerItem(MenuItem menuItem) {
        // Create a new fragment and specify the planet to show based on
        // position
        Fragment fragment = null;

        Class fragmentClass;
        switch(menuItem.getItemId()) {
            case R.id.nav_first_fragment:
                fragmentClass = MainActivity.class;
                break;
            case R.id.nav_second_fragment:
                fragmentClass = SecondActivity.class;
                break;
            case R.id.nav_third_fragment:
                fragmentClass = ThirdActivity.class;
                break;
            default:
                fragmentClass = MainActivity.class;
        }

        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

        // Highlight the selected item, update the title, and close the drawer
        menuItem.setChecked(true);
        setTitle(menuItem.getTitle());
        mDrawer.closeDrawers();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawer.openDrawer(GravityCompat.START);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

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

SecondActivity.java

 private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        selectDrawerItem(menuItem);
                        return true;
                    }
                });
    }

    public void selectDrawerItem(MenuItem menuItem) {
        // Create a new fragment and specify the planet to show based on
        // position
        Fragment fragment = null;

        Class fragmentClass;
        switch(menuItem.getItemId()) {
            case R.id.nav_first_fragment:
                fragmentClass = MainActivity.class;
                break;
            case R.id.nav_second_fragment:
                fragmentClass = SecondActivity.class;
                break;
            case R.id.nav_third_fragment:
                fragmentClass = ThirdActivity.class;
                break;
            default:
                fragmentClass = SecondActivity.class;
        }

        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

        // Highlight the selected item, update the title, and close the drawer
        menuItem.setChecked(true);
        setTitle(menuItem.getTitle());
        mDrawer.closeDrawers();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawer.openDrawer(GravityCompat.START);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    // @Override
    // protected void onPostCreate(Bundle savedInstanceState) {
    //     super.onPostCreate(savedInstanceState);
    // }

2 个答案:

答案 0 :(得分:0)

您的问题不明确,因为您尚未发布完整的课程代码,以及您获得的错误。

我所理解的是,当您选择导航抽屉中的项目时,您想要更改片段。与 Gmail 应用类似,当用户从收件箱文件夹移动到已发送文件夹或其他任何文件夹时。

如果您尝试执行相同操作,则需要创建一个Activity,插入一个片段。选择导航抽屉中的项目时,应根据导航抽屉中选择的选项将新片段替换为当前片段。

检查以下两个链接,它们将为您提供全部想法。

Link1:https://androidbelieve.com/material-navigation-drawer-with-header/

Link2:http://joerichard.net/android/android-how-to-change-fragments-when-navigation-drawer-item-selected/

答案 1 :(得分:0)

FragmentTransaction.replace 调用remove(Fragment)然后调用add(int, Fragment, String)来实际替换片段。因此,在您的情况下,容器视图中可能会出现 no 片段,因此您正在调用remove(null)。因此,请检查是否必须添加或替换片段。

您可以检查容器视图中是否有任何片段并相应地继续:

 FragmentManager fragmentManager = getSupportFragmentManager();
 android.support.v4.app.Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.flContent);
 if(currentFragment==null){ 
    fragmentManager.beginTransaction().add(R.id.flContent, fragment).commit();
 }else{
   fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
 }

希望这能解决您的问题。