我已经实现了一个材料导航抽屉,我想在选择导航抽屉中的一个菜单项而不是配置文件活动的片段时启动一个活动。我不想在ProfileActivity中显示导航抽屉。我只想在从导航抽屉中选择菜单项时启动ProfileActivity。其他菜单项打开了我可以接受的片段。如果我在switch case语句中调用Intent,应用程序就会崩溃,因为我正在替换片段。
我目前使用的方法是
我打开FragmentProfile,在该片段中我开始一个意图,打开Profileactivity。
主活动公共类MainActivity扩展了AppCompatActivity{
private DrawerLayout mDrawerLayout;
@Optional @InjectView(R.id.username)
TextView _username;
public String Username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
navigationView.getMenu().performIdentifierAction(R.id.nav_dashboard, 0);
Username = SingletonName.instance().Username;
_username.setText(Username);
}
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 = null;
switch (menuItem.getItemId()) {
case R.id.nav_dashboard:
fragmentClass = DashboardActivity.class;
// Intent dashboard = new Intent(this, DashboardActivity.class);
// startActivity(dashboard);
break;
case R.id.nav_history:
Intent historyintent = new Intent(this, HistoryActivity.class);
startActivity(historyintent);
break;
case R.id.nav_Flags:
// fragmentClass = ThirdFragment.class;
break;
case R.id.nav_Profile:
fragmentClass = FragmentProfile.class;
break;
case R.id.nav_Contacts:
fragmentClass = FragmentContact.class;
break;
case R.id.nav_Alerts:
break;
case R.id.nav_Logout:
Intent loginintent = new Intent(this, LoginActivity.class);
startActivity(loginintent);
break;
default:
fragmentClass = DashboardActivity.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = this.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());
mDrawerLayout.closeDrawers();
}
@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.
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
//noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
return super.onOptionsItemSelected(item);
}
}
FragmentProfile片段公共类FragmentProfile扩展了片段{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Intent profile = new Intent(getActivity(), ProfileActivity.class);
startActivity(profile);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
/***由s210121629于2015年7月1日创建。*/ProfileActivity公共类ProfileActivity扩展了ActionBarActivity{
// Declaring Your View and Variables
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[] = {"Personal Info", "Medical Info", "Contact Info"};
int Numboftabs = 3;
String username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
// Creating The Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.mipmap.ic_action_back);
ab.setDisplayHomeAsUpEnabled(true);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
username = getIntent().getStringExtra("username");
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.md_cyan_900);
}
});
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
Bundle bundle = new Bundle();
bundle.putString("message", value );
PersonalinfoFragment fragInfo = new PersonalinfoFragment();
fragInfo.setArguments(bundle);
}
}
@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();
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
有没有更简单更干净的方法。
当前错误
Process: com.example.s210121629.myhealth, PID: 31073
java.lang.NullPointerException
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:417)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:452)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:444)
at com.example.s210121629.myhealth.MainActivity.selectDrawerItem(MainActivity.java:151)
at com.example.s210121629.myhealth.MainActivity$1.onNavigationItemSelected(MainActivity.java:72)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:136)
at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.design.internal.NavigationMenuPresenter.onItemClick(NavigationMenuPresenter.java:179)
at android.widget.AdapterView.performItemClick(AdapterView.java:308)
at android.widget.AbsListView.performItemClick(AbsListView.java:1524)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3531)
at android.widget.AbsListView$3.run(AbsListView.java:4898)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
有没有更简单更干净的方法。
IMO是的,但在现阶段需要大量重写。关于快速修复,从您的代码看,解决方案可能涉及以下部分:
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager = this.getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
如果为空,则用复选框包围:
if (fragment != null) {
//...
}
由于从未为片段
赋值,因此应跳过实际加载片段的部分。
而不是在切换案例的末尾使用片段管理器。我更改了每个案例中的每个片段。
public void selectDrawerItem(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_dashboard:
getSupportFragmentManager().beginTransaction()
.replace(R.id.flContent, new DashboardActivity())
.commit();
break;
// case R.id.nav_history:
//
//
// Intent historyintent = new Intent(this, HistoryActivity.class);
// startActivity(historyintent);
// break;
case R.id.nav_Flags:
// fragmentClass = ThirdFragment.class;
break;
case R.id.nav_Profile:
Intent profileActivity = new Intent(getApplicationContext(), ProfileActivity.class);
startActivity(profileActivity);
break;
case R.id.nav_Contacts:
getSupportFragmentManager().beginTransaction()
.replace(R.id.flContent, new FragmentContact())
.commit();
break;
case R.id.nav_Alerts:
break;
case R.id.nav_Logout:
Intent loginintent = new Intent(this, LoginActivity.class);
startActivity(loginintent);
break;
default:
getSupportFragmentManager().beginTransaction()
.replace(R.id.flContent, new DashboardActivity())
.commit();
}
你好,我想创建一个与导航抽屉活动在Android Studio点击事件。当我在左菜单中单击Gallery时,我有意将其重定向到Gallery activity。这是好的,它的工作,但没有留下菜单在画廊的活动。我希望所有重定向的活动已经离开菜单。我怎么能这么做? 我只添加了intent line,其他的都是我选择NavigationDrawerActivity时的默认代码。 画廊布局
我是android新手。我想实现一个由项目列表组成的导航抽屉,单击它会打开一个新活动。基本上是所有活动的导航抽屉。当我从导航抽屉中选择一个项目时,特定的活动就会打开。导航抽屉代码是通过执行空活动来实现的。我想在所有活动中实现导航抽屉功能,这些活动被视为空活动,这些活动已经有了一些功能,导航抽屉功能也可以工作。请帮帮我。 这是activity_header档案 这是我的主要活动 这是头活动java代
我是android的初学者。我想创建一个导航菜单抽屉。当我点击图标时,它会打开。当我点击菜单项时,它会显示片段。但是当我点击菜单项时,我的导航抽屉不会自动关闭。每次我关上导航抽屉看我的片段。我不知道在哪里更改代码?随信附上我的主要活动。java,activity_main。xml。任何人都可以帮助我。 activity_main.xml 主要活动。Java语言
你好,我有一个关于Android导航抽屉的问题。我在我的应用程序中包含了导航抽屉,一切都很顺利,但我想将图像添加到列表中的每个菜单中。 我是Android新手,所以如果有人能帮助我,我将不胜感激,这是导航抽屉的源代码 http://javatechig.com/android/navigation-drawer-android-example 这是我的WebViewFragments文件 这是我的
出于某种原因,在抽屉外轻触时,导航抽屉不会关闭。它是MainActivity中带有ListView的另一个片段: 当点击右边的片段时,它不会关闭抽屉,相反,它的行为就像片段占据了整个屏幕,而点击侦听器在片段中仍然处于活动状态。 activity_main.xml: MainActivity.java: 不确定我做错了什么,因为在抽屉区域外敲击时关闭抽屉应该是抽屉布局中的默认行为。
这是点击事件。 这是xml 结果将显示在片段中,但抽屉菜单仍然显示。我该怎么做才能关闭它?任何人都给我建议,如何解决这个问题,任何帮助非常感谢。谢谢你。