Java Android-导航抽屉-片段加载-如何

Java Android-导航抽屉-片段加载-如何,java,android,Java,Android,嗨,我的导航抽屉有问题。 我已经按照教程完成了这段代码 我试图在菜单中单击时加载不同的片段。我只能先装一个 我想得到一些关于如何加载其余片段的建议——这样它们就不会只显示Toast消息 但我在这方面没有任何成功。有什么建议吗 MainActivity.java 嗨,谢谢你的回答。我试过类似的东西。但是我的fragment/newFragment出现了一个错误。我得到了一个:错误的第二个参数类型。这删除了newFragment错误,但代码的洞行是:FragmentTransaction=getSu

嗨,我的导航抽屉有问题。 我已经按照教程完成了这段代码

我试图在菜单中单击时加载不同的片段。我只能先装一个

我想得到一些关于如何加载其余片段的建议——这样它们就不会只显示Toast消息

但我在这方面没有任何成功。有什么建议吗

MainActivity.java


嗨,谢谢你的回答。我试过类似的东西。但是我的fragment/newFragment出现了一个错误。我得到了一个:错误的第二个参数类型。这删除了newFragment错误,但代码的洞行是:FragmentTransaction=getSupportFragmentManager.beginTransaction;现在是这样一个错误我没有收到任何错误-但是按下该项会导致应用程序崩溃案例R.id.poi:Toast.makeTextgetApplicationContext,选中兴趣点,Toast.LENGTH\u SHORT.show;PoiFragment newFragment=新PoiFragment;android.support.v4.app.FragmentTransaction=getSupportFragmentManager.beginTransaction;transaction.replaceR.id.poi,newFragment;transaction.addToBackStacknull;哇,我一定累了-对不起-谢谢你的帮助和耐心-现在可以了。你修好了吗@Vikrams Inxt解决方案应该适合您。@SumighoshCharuvil-不,我没有。它继续使用newFragment声明错误。
    import android.support.design.widget.NavigationView;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity {
    //Defining Variables
    private Toolbar toolbar;
    private NavigationView navigationView;
    private DrawerLayout drawerLayout;

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

        // Initializing Toolbar and setting it as the actionbar
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Initializing NavigationView
        navigationView = (NavigationView) findViewById(R.id.navigation_view);

        //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {


                //Checking if the item is in checked state or not, if not make it in checked state
                if(menuItem.isChecked()) menuItem.setChecked(false);
                else menuItem.setChecked(true);

                //Closing drawer on item click
                drawerLayout.closeDrawers();

                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()){


                    //Replacing the main content with ClubFragment Which is our Club View;
                    case R.id.club:
                        Toast.makeText(getApplicationContext(),"Club",Toast.LENGTH_SHORT).show();
                        ClubFragment fragment = new ClubFragment();
                        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.frame,fragment);
                        fragmentTransaction.commit();
                        return true;

                    // For rest of the options we just show a toast on click

                    case R.id.poi:
                        Toast.makeText(getApplicationContext(),"Points of Interest Selected",Toast.LENGTH_SHORT).show();

                        return true;

                    case R.id.calender:
                        Toast.makeText(getApplicationContext(),"Calender Selected",Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.help:
                        Toast.makeText(getApplicationContext(),"Help Selected",Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.share:
                        Toast.makeText(getApplicationContext(),"Share Selected",Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.rate:
                        Toast.makeText(getApplicationContext(),"Rate Us Selected",Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.settings:
                        Toast.makeText(getApplicationContext(),"Settings Selected",Toast.LENGTH_SHORT).show();
                        return true;
                    default:
                        Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show();
                        return true;

                }
            }
        });

        // Initializing Drawer Layout and ActionBarToggle
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

                super.onDrawerOpened(drawerView);
            }
        };

        //Setting the actionbarToggle to drawer layout
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        //calling sync state is necessay or else your hamburger icon wont show up
        actionBarDrawerToggle.syncState();






    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Like you had done in R.id.club you can open other fragments on click of R.id.poi or R.id.Calendal
Fragment newFragment = new ABC(); 
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);

    // Commit the transaction
    transaction.commit();