gpt4 book ai didi

android - 从 Material 抽屉导航启动 Activity ,该抽屉具有用于 fragment 菜单项的开关盒

转载 作者:行者123 更新时间:2023-11-29 20:30:46 25 4
gpt4 key购买 nike

我已经实现了一个 Material Navigation Drawer,我想在选择抽屉导航中的一个菜单项而不是 Profile Activity 的 fragment 时启动一个 Activity。我不想在 ProfileActivity 中显示抽屉导航。当我从抽屉导航中选择菜单项时,我只想启动 ProfileActivity。其他菜单项打开了我可以接受的 fragment 。如果我在 switch case 语句中调用 Intent,应用程序会崩溃,因为我正在替换 fragment 。

我目前使用的方法是

我打开 FragmentProfile 并在该 fragment 中启动一个打开 Profileactivity 的 Intent。

主要 Activity
公共(public)类 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 fragment 公共(public)类 FragmentProfile 扩展 fragment {

@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-07-01。 */ProfileActivity 公共(public)类 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)

最佳答案

而不是在切换案例的末尾使用 fragment 管理器。我更改了每个案例中的每个 fragment 。

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 - 从 Material 抽屉导航启动 Activity ,该抽屉具有用于 fragment 菜单项的开关盒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32029068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com