Android Studio 的导航问题引发错误 java.lang.IllegalArgumentException



所以我一直在尝试让一个按钮工作并导航到另一个片段,但只能从那个按钮访问,我想把它从底部导航栏中分离出来,那个按钮可以

我发现的主要问题是,当试图像添加底部导航一样添加这些操作时,它会抛出java.lang.IllegalArgumentException:ID没有引用该活动中的视图

有人能给我指一个正确的方向,告诉我做错了什么吗?

我认为这与导航主机碎片的混淆有关?也许他们认为这是一样的,并且互相覆盖?我一直在兜圈子,禁用底部导航栏也不能解决问题。

这是我的主活动代码,以及带有我想要执行此操作的按钮的java片段。

public class MainActivity extends AppCompatActivity {
private AppBarConfiguration appBarConfiguration;
ActivityMainBinding binding;
BottomNavigationView bottomNavigationView; //initializing the bottom nav bar from activity_main again
@Override
protected void onCreate(Bundle savedInstanceState) {//changing content which is based on the activity_main class, think of activity_main as the  main driver class, where everything is activated at
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
//FROM HERE
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
//TO HERE THESE PARTS THROW THE EXCEPTIONS

@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
}

除了我的导航图的代码外,其中的其他操作都用于我的底部导航,这就是为什么一个片段有2个操作,1个按钮,与同一个导航图共享

<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/my_nav"
app:startDestination="@id/fl1">
<fragment
android:id="@+id/fl1"
android:name="com.example.appnav.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first" >
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/fl2" />
<!--These fragments (pages) are all linked by the action button with where they end up on click -->
</fragment>
<fragment
android:id="@+id/fl2"
android:name="com.example.appnav.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" >
<action
android:id="@+id/action_secondFragment_to_frag3"
app:destination="@id/fl3" />
<action
android:id="@+id/action_fl2_to_flp"
app:destination="@id/flp" />
</fragment>
<fragment
android:id="@+id/fl3"
android:name="com.example.appnav.frag3"
android:label="fragment_frag3"
tools:layout="@layout/fragment_frag3" >
<action
android:id="@+id/action_fl3_to_fl4"
app:destination="@id/fl4" />
</fragment>
<fragment
android:id="@+id/fl4"
android:name="com.example.appnav.frag4"
android:label="fragment_frag4"
tools:layout="@layout/fragment_frag4" >
<action
android:id="@+id/action_fl4_to_fl5"
app:destination="@id/fl5" />
</fragment>

<fragment
android:id="@+id/fl5"
android:name="com.example.appnav.fragt"
android:label="fragment_fragt"
tools:layout="@layout/fragment_temp"/>
<fragment
android:id="@+id/flp"
android:name="com.example.appnav.pay_frag"
android:label="pay_frag"
tools:layout="@layout/pay_frag" >
<action
android:id="@+id/action_flp_to_fl_pdone"
app:destination="@id/fl_pdone" />
</fragment>
<fragment
android:id="@+id/fl_pdone"
android:name="com.example.appnav.fl_pdone"
android:label="fragment_fl_pdone"
tools:layout="@layout/fragment_fl_pdone" />

</navigation>

这是包含按钮的片段基本上是尝试使用navgraph中的操作,并将u带到下一个片段,在这种情况下是一个不在底部导航上的片段

public class SecondFragment extends Fragment {
private FragmentSecondBinding binding;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
binding = FragmentSecondBinding.inflate(inflater, container, false);
return binding.getRoot();
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.buttonSecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(SecondFragment.this)
.navigate(R.id.action_fl2_to_flp);
}
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}

NavController想要view而不是context。只需将您的context更改为view,如下所示。

binding.buttonSecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NavHostFragment.findNavController(view)
.navigate(R.id.action_fl2_to_flp);
}
});

如果没有解决,请告诉我。

相关内容

最新更新