[英]Navigating inside a floating action button
I have added Main activity.我添加了主要活动。 I have a FAB button and i am trying to navigate to one of the fragments on click of the button.我有一个 FAB 按钮,我试图通过单击按钮导航到其中一个片段。 But I receive java.lang.IllegalArgumentException: navigation destination XXXXX/galleryFragment is unknown to this NavController Not sure what is the reason for the exception.但是我收到java.lang.IllegalArgumentException: navigation destination XXXXX/galleryFragment is unknown to this NavController不确定异常的原因是什么。 I am new to android development please excuse my ignorance我是安卓开发新手,请原谅我的无知
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
FloatingActionButton fab = findViewById(R.id.fab);
Navigation.setViewNavController(fab, navController);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.galleryFragment);
}
});
}
Navigation.findNavController(view)
only works when the View is within the NavHostFragment
(ie, it is a view created by one of your fragments). Navigation.findNavController(view)
仅在视图位于NavHostFragment
内NavHostFragment
(即,它是由您的片段之一创建的视图)。
If your FloatingActionButton
is outside of the NavHostFragment
(such as in your activity's layout), then you need to use the same thing you're using in onCreate()
: Navigation.findNavController(this, R.id.nav_host_fragment)
:如果您的FloatingActionButton
位于NavHostFragment
之外(例如在您的活动布局中),那么您需要使用您在onCreate()
使用的相同内容: Navigation.findNavController(this, R.id.nav_host_fragment)
:
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.galleryFragment);
}
});
you can store the navController
as a global variable, then don't use Navigation.findNavController
, use navController.navigate
.您可以将navController
存储为全局变量,然后不要使用Navigation.findNavController
,使用navController.navigate
。
NavController navController = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
navController.navigate(R.id.galleryFragment);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.