在导航抽屉上调用不是片段活动的活动

Posted

技术标签:

【中文标题】在导航抽屉上调用不是片段活动的活动【英文标题】:Call an Activity that is not a fragment activity on Navigation Drawer 【发布时间】:2022-01-10 13:07:02 【问题描述】:

我是 android Studio 的新手。我有一个导航抽屉,我试图调用一个不是片段活动的活动,我对此感到困惑。我想调用 RecipeActivity 以添加到导航抽屉中,但该活动扩展了 AppCompatActivity 而不是片段。我该如何解决这个问题?

RecipeActivity.java

public class RecipeActivity extends AppCompatActivity 

    List<Recipe> listRecipe;


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

        

        RecyclerView myRv = (RecyclerView) findViewById(R.id.recyclerview_id);
        RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, listRecipe);
        myRv.setLayoutManager(new GridLayoutManager(this, 2));
        myRv.setAdapter(myAdapter);
    


HomePage.java 包含导航抽屉代码

public class HomePage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener 
    private DrawerLayout drawer;

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_draw_open, R.string.navigation_draw_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        if (savedInstanceState == null)

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

        navigationView.setCheckedItem((R.id.nav_home));
        

    

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) 
        switch(item.getItemId())
            case R.id.nav_home:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new HomeFragment()).commit();
                break;
            case R.id.nav_recipe:
                //startActivity(new Intent(HomePage.this, RecipeActivity.class));


                startActivity(new Intent(getActivity(), RecipeActivity.class));
               //getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                     //  new RecipeActivity()).commit();

               // Intent newIntent = new Intent(HomePage.this, RecipeActivity.class);
               // startActivity(newIntent);
                break;

            case R.id.nav_logout:
                Toast.makeText(this, "Logged out", Toast.LENGTH_SHORT).show();
                 break;
        

        drawer.closeDrawer(GravityCompat.START);
        return true;
    

    @Override
    public void onBackPressed() 
        if (drawer.isDrawerOpen(GravityCompat.START))
            drawer.closeDrawer(GravityCompat.START);
         else 
            super.onBackPressed();
        
        super.onBackPressed();
    

谢谢。

【问题讨论】:

【参考方案1】:

改变你的startActivity(new Intent(getActivity(), RecipeActivity.class));startActivity(new Intent(HomePage.this, RecipeActivity.class));

并确保所有活动都添加到AndroidManifest.xml

【讨论】:

我忘记了,这解决了我的问题。谢谢!

以上是关于在导航抽屉上调用不是片段活动的活动的主要内容,如果未能解决你的问题,请参考以下文章

想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉

将一个片段调用到另一个片段时更改标题TextView

如何在主片段中单击按钮时打开导航抽屉?

Android导航抽屉点击事件问题

导航抽屉活动:在按钮单击时从片段移动到片段

java 导航抽屉切换活动而不是片段。字体:https://stackoverflow.com/questions/19442378/navigation-drawer-to-switch-