这里有一个收集我的片段的导航。我想在这些片段之间切换,但是通过单击菜单项
NavDirections action = AddArtFragmentDirections.actionAddArtFragmentToViewArtFragment();
Navigation.findNavController(view).navigate(action);
我知道它是用做的,但我不能用onoptionsitemselected
这是所有的密码
public class MainActivity extends AppCompatActivity {
private FirebaseAuth auth;
private FirebaseUser currentUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
auth = FirebaseAuth.getInstance();
currentUser = auth.getCurrentUser();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = new MenuInflater(this);
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.addArt) {
NavDirections action = AddArtFragmentDirections.actionAddArtFragmentToViewArtFragment();
Navigation.findNavController(this, R.id.addArtFragment).navigate(action);
} else if (item.getItemId() == R.id.seeUsers) {
//open userFragment
} else if (item.getItemId() == R.id.signOut) {
auth.signOut();
Intent loginActivity = new Intent(this, LoginAndRegisterActivity.class);
startActivity(loginActivity);
finish();
}
return super.onOptionsItemSelected(item);
}
}
和graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_graph"
app:startDestination="@id/viewArtFragment">
<fragment
android:id="@+id/addArtFragment"
android:name="com.onurerkantoros.artbookfragment.MainFragments.AddArtFragment"
android:label="fragment_add_art"
tools:layout="@layout/fragment_add_art" >
<action
android:id="@+id/action_addArtFragment_to_viewArtFragment"
app:destination="@id/viewArtFragment" />
</fragment>
<fragment
android:id="@+id/viewArtFragment"
android:name="com.onurerkantoros.artbookfragment.MainFragments.ViewAllArtFragment"
android:label="fragment_view_art"
tools:layout="@layout/fragment_view_art" >
<action
android:id="@+id/action_viewArtFragment_to_addArtFragment2"
app:destination="@id/addArtFragment" />
<action
android:id="@+id/action_viewArtFragment_to_usersFragment"
app:destination="@id/usersFragment" />
<action
android:id="@+id/action_viewArtFragment_to_artFragment"
app:destination="@id/artFragment" />
</fragment>
<fragment
android:id="@+id/usersFragment"
android:name="com.onurerkantoros.artbookfragment.MainFragments.UsersFragment"
android:label="fragment_users"
tools:layout="@layout/fragment_users" >
<action
android:id="@+id/action_usersFragment_to_viewArtFragment"
app:destination="@id/viewArtFragment" />
</fragment>
<fragment
android:id="@+id/artFragment"
android:name="com.onurerkantoros.artbookfragment.MainFragments.ArtFragment"
android:label="fragment_art"
tools:layout="@layout/fragment_art" >
<action
android:id="@+id/action_artFragment_to_viewArtFragment"
app:destination="@id/viewArtFragment" />
</fragment>
</navigation>
如果我这样做 Navigation.findNavController(this, R.id.addArtFragment).navigate(action);
我得到一个错误java.lang.illegalargumentexception:id没有引用这个活动中的视图
菜单图像
我的导航图
1条答案
按热度按时间lh80um4z1#
请确保将下面的上下文传递到“activity\u main.xml”中。代码似乎找不到方法来处理这些片段。
如果可以的话,请发布更多关于这个错误的日志,这样我们可以更具体一些。
另外,如果您想了解更多关于片段导航的信息,请参阅本教程https://www.youtube.com/watch?v=tpv8xa7m-iw&t=320s. 你把所有的片段分开处理,直接在java类中操作它们就更容易了(在你的例子中是mainactivity)。