[英]Passing from one fragment to another
我在這里嘗試創建一個菜單,該菜單在單擊時會從一個片段傳遞到另一個片段。 當我按菜單中的特定項目(活動)時,活動將退出,故障是
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();// this line has the problem
這是我完整的日志:
FATAL EXCEPTION: main java.lang.NullPointerException
在android.support.v4.app.BackStackRecord.replace上的android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:449)上的android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:414) (BackStackRecord.java:441)atbr.exemplozxingintegration.MainActivity.selectDrawerItem(MainActivity.java:84 atbr.exemplozxingintegration.MainActivity $ 1.onNavigationItemSelected(MainActivity.java:50)在android.support.design.widget.NavigationView $ 1.onMenuViewSelected(Navigation .java:150),位於android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811),位於android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)。在android.support.design.internal.NavigationMenuPresenter $ 1.onClick(NavigationMenuPresenter.java:308)上的support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)在android.view.View.performClick(View.java) :4439),位於android.view.View $ PerformClick.run(View.java:18395),位於android.os.Handler.handleCallback(Handler.java) :725)在android.os.Handler.dispatchMessage(Handler.java:92)在android.os.Looper.loop(Looper.java:176)在android.app.ActivityThread.main(ActivityThread.java:5319)在Java .lang.reflect.Method.invokeNative(本機方法)位於com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)上的java.lang.reflect.Method.invoke(Method.java:511)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)在dalvik.system.NativeStart.main(本機方法)
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
txResult = (TextView) findViewById(R.id.txResult);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
nvDrawer = (NavigationView) findViewById (R.id.nvView);
setupDrawerContent(nvDrawer);}
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;
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
fragmentClass = MainActivity.class;
break;
case R.id.nav_second_fragment:
fragmentClass = SecondActivity.class;
break;
case R.id.nav_third_fragment:
fragmentClass = ThirdActivity.class;
break;
default:
fragmentClass = MainActivity.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = 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());
mDrawer.closeDrawers();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
switch (item.getItemId()) {
case android.R.id.home:
mDrawer.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
// @Override
// protected void onPostCreate(Bundle savedInstanceState) {
// super.onPostCreate(savedInstanceState);
//
SecondActivity.java
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;
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
fragmentClass = MainActivity.class;
break;
case R.id.nav_second_fragment:
fragmentClass = SecondActivity.class;
break;
case R.id.nav_third_fragment:
fragmentClass = ThirdActivity.class;
break;
default:
fragmentClass = SecondActivity.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = 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());
mDrawer.closeDrawers();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
switch (item.getItemId()) {
case android.R.id.home:
mDrawer.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
// @Override
// protected void onPostCreate(Bundle savedInstanceState) {
// super.onPostCreate(savedInstanceState);
// }
您的問題尚不清楚,因為您尚未發布完整的課程代碼,以及遇到了什么錯誤。
我了解的是,當在導航抽屜中選擇一個項目時,您想更改片段。 與Gmail應用類似,當用戶從收件箱文件夾移至已發送文件夾或任何其他文件夾時。
如果您嘗試執行相同的操作,則需要進行一個“活動”,然后插入一個片段。 選擇導航抽屜中的某個項目時,應根據導航抽屜中選擇的選項將當前片段替換為新片段。
檢查以下兩個鏈接,這將為您提供完整的想法。
鏈接1: https : //androidbelieve.com/material-navigation-drawer-with-header/
鏈接2: http : //joerichard.net/android/android-how-to-change-fragments-when-navigation-drawer-item-selected/
FragmentTransaction.replace調用remove(Fragment)
,然后調用add(int, Fragment, String)
實際替換片段。 因此,在您的情況下,可能會發生以下情況:容器視圖中沒有片段,因此您正在調用remove(null)
。 因此,請檢查是否必須添加或替換片段。
您可以檢查容器視圖中是否有任何片段,然后進行相應的操作:
FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.flContent);
if(currentFragment==null){
fragmentManager.beginTransaction().add(R.id.flContent, fragment).commit();
}else{
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
}
希望這能解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.