我是android的初学者。我想打开另一个活动类NewTest.class onclick导航抽屉的列表项。但我得到了 /com.navigation E / MainActivity:创建片段时出错。请告诉我这有什么问题?谢谢。 这是我的MainActivity.class代码
...
//Let's say I want to show a MessageBox containing if the Service is connected to Server
MyService myservice = new MyService();
MessageBox.Show("Client Status: " + myservice.isConnected());
答案 0 :(得分:0)
您可以使用onNavigationItemSelected()方法。
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)
{
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.idofYourNavmenu)
{
Intent intent = new Intent(CurrentActivity.this, ActivityToCall.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
答案 1 :(得分:0)
你可以达到如下目的:
Fragment fragment = null;
Class fragmentClass = null;
if (id == R.id.nav_1) {
fragmentClass = demo1.class;
} else if (id == R.id.nav_2) {
//pass Activity class where you want to navigate
fragmentClass=CredentialActivity.class;
}
try {
if(fragmentClass.newInstance() instanceof Fragment) {
fragment = (Fragment) fragmentClass.newInstance();
if (fragmentClass.getSimpleName().equals("DailyReport")) {
Bundle bundle = new Bundle();
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.home_layout, fragment).commit();
// Highlight the selected item has been done by NavigationView
item.setChecked(true);
// Set action bar title
setTitle(item.getTitle());
// Close the navigation drawer
drawer.closeDrawers();
}
}else
{
// After logout redirect user to Loing Activity
Intent i = new Intent(HomeActivity.this, fragmentClass);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Start Login Activity
startActivity(i);
finish();
}