在 Navigation Drawer 上调用不是片段 activity 的 Activity
Call an Activity that is not a fragment activity on Navigation Drawer
我是 Android Studio 的新手。我有一个导航抽屉,我正在尝试调用一个 activity ,它不是一个片段 activity ,我对此感到困惑。我想调用 RecipeActivity 以添加到导航抽屉中,但是 activity 扩展了 AppCompatActivity 而不是 Fragment。我该如何解决这个问题?
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();
}
}
谢谢。
改变你的
startActivity(new Intent(getActivity(), RecipeActivity.class));
至 startActivity(new Intent(HomePage.this, RecipeActivity.class));
并确保所有活动都添加到 AndroidManifest.xml
我是 Android Studio 的新手。我有一个导航抽屉,我正在尝试调用一个 activity ,它不是一个片段 activity ,我对此感到困惑。我想调用 RecipeActivity 以添加到导航抽屉中,但是 activity 扩展了 AppCompatActivity 而不是 Fragment。我该如何解决这个问题?
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();
}
}
谢谢。
改变你的
startActivity(new Intent(getActivity(), RecipeActivity.class));
至 startActivity(new Intent(HomePage.this, RecipeActivity.class));
并确保所有活动都添加到 AndroidManifest.xml