此 NavController 不知道导航 [destination_name]
Posted
技术标签:
【中文标题】此 NavController 不知道导航 [destination_name]【英文标题】:Navigation [destination_name] is unknown to this NavController 【发布时间】:2020-03-01 14:43:51 【问题描述】:我已经多次看到这个问题,但主要来自需要从 C 回到 A 的人,但我的问题是相反的。
这是我的导航图的样子(部分)
HOME -> NestedGraph Account ->此嵌套图表帐户内的嵌套图表联系人。
我想做的是从 HOME 转到联系人。 我已经尝试了 xml 导航图中每个片段的全局操作,并且每次遇到相同的错误:
java.lang.IllegalArgumentException: navigation destination com.example.techcrea:id/contactsList referenced from action com.example.techcrea:id/action_global_contactsList4 is unknown to this NavController
at androidx.navigation.NavController.navigate(NavController.java:789)
at androidx.navigation.NavController.navigate(NavController.java:730)
at androidx.navigation.NavController.navigate(NavController.java:716)
at androidx.navigation.NavController.navigate(NavController.java:704)
**at com.example.techcrea.Fragments.Home.Home.onClick(Home.java:126)**
at android.view.View.performClick(View.java:6329)
at android.view.View$PerformClick.run(View.java:25002)
at android.os.Handler.handleCallback(Handler.java:809)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7555)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
但是当我从帐户“简单”操作中使用“导航”访问“联系人”时,它可以正常工作。只有 Home 的“快捷方式”不起作用。
这是我的导航图,我将全局操作放在上面,但我尝试将它放在此 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/navgraph_bottomview"
app:startDestination="@id/mnuHome">
<action
android:id="@+id/action_global_contactsList4"
app:destination="@id/contactsList"/>
<fragment
android:id="@id/mnuHome"
android:name="com.example.techcrea.Fragments.Home.Home"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_mnuHome_to_accountMain"
app:destination="@id/mnuAccount" />
<action
android:id="@+id/action_mnuHome_to_mnuBills"
app:destination="@id/mnuBills" />
<action
android:id="@+id/action_mnuHome_to_mnuContracts"
app:destination="@id/mnuContracts" />
<action
android:id="@+id/action_mnuHome_to_mnuHelp"
app:destination="@id/mnuHelp" />
</fragment>
<fragment
android:id="@id/mnuBills"
android:name="com.example.techcrea.Fragments.Bills.BillsMain"
android:label="fragment_bills_main"
tools:layout="@layout/fragment_bills_main" />
<fragment
android:id="@id/mnuContracts"
android:name="com.example.techcrea.Fragments.Contracts.ContractsMain"
android:label="fragment_contracts_main"
tools:layout="@layout/fragment_contracts_main" />
<navigation
android:id="@+id/mnuHelp"
app:startDestination="@id/mnuHelp" >
<fragment
android:id="@id/mnuHelp"
android:name="com.example.techcrea.Fragments.Help.Help"
android:label="fragment_help"
tools:layout="@layout/fragment_help">
<action
android:id="@+id/action_mnuHelp_to_helpStep2"
app:destination="@id/helpStep2" />
</fragment>
<fragment
android:id="@+id/helpStep2"
android:name="com.example.techcrea.Fragments.Help.HelpStep2"
android:label="fragment_help_step2"
tools:layout="@layout/fragment_help_step2">
<action
android:id="@+id/action_helpStep2_to_helpStep3A"
app:destination="@id/helpStep3A" />
<action
android:id="@+id/action_helpStep2_to_helpStep3B"
app:destination="@id/helpStep3B" />
</fragment>
<fragment
android:id="@+id/helpStep3B"
android:name="com.example.techcrea.Fragments.Help.HelpStep3B"
android:label="fragment_help_step3_b"
tools:layout="@layout/fragment_help_step3_b" />
<fragment
android:id="@+id/helpStep3A"
android:name="com.example.techcrea.Fragments.Help.HelpStep3A"
android:label="fragment_help_step3"
tools:layout="@layout/fragment_help_step3" />
</navigation>
<navigation
android:id="@+id/mnuAccount"
app:startDestination="@id/mnuAccount" >
<fragment
android:id="@id/mnuAccount"
android:name="com.example.techcrea.Fragments.Account.AccountMain"
android:label="fragment_account_main"
tools:layout="@layout/fragment_account_main">
<action
android:id="@+id/action_mnuAccount_to_accountAboutUs"
app:destination="@id/accountAboutUs" />
<action
android:id="@+id/action_mnuAccount_to_accountChangePassword"
app:destination="@id/accountChangePassword" />
<action
android:id="@+id/action_mnuAccount_to_accountEditInformations"
app:destination="@id/accountEditInformations" />
<action
android:id="@+id/action_mnuAccount_to_contactsList"
app:destination="@id/contacts" />
</fragment>
<navigation
android:id="@+id/contacts"
app:startDestination="@id/contactsList">
<fragment
android:id="@+id/contactDisplayOne"
android:name="com.example.techcrea.Fragments.Account.ContactDisplayOne"
android:label="fragment_contact_display_one"
tools:layout="@layout/fragment_contact_display_one">
<action
android:id="@+id/action_contactDisplayOne_to_contactEditOne"
app:destination="@id/contactEditOne" />
</fragment>
<fragment
android:id="@+id/contactAddOne"
android:name="com.example.techcrea.Fragments.Account.ContactAddOne"
android:label="fragment_contact_add_one"
tools:layout="@layout/fragment_contact_add_one" >
<action
android:id="@+id/action_contactAddOne_to_contactsList"
app:destination="@id/contactsList"
app:popUpTo="@+id/contactAddOne" />
</fragment>
<fragment
android:id="@+id/contactsList"
android:name="com.example.techcrea.Fragments.Account.ContactsList"
android:label="fragment_account_watchcontacts"
tools:layout="@layout/fragment_account_watchcontacts">
<action
android:id="@+id/action_contactsList_to_contactAddOne"
app:destination="@id/contactAddOne"
app:popUpTo="@+id/contacts" />
<action
android:id="@+id/action_contactsList_to_contactEditOne"
app:destination="@id/contactEditOne" />
<action
android:id="@+id/action_contactsList_to_contactDisplayOne"
app:destination="@id/contactDisplayOne" />
</fragment>
<fragment
android:id="@+id/contactEditOne"
android:name="com.example.techcrea.Fragments.Account.ContactEditOne"
android:label="fragment_contact_edit_one"
tools:layout="@layout/fragment_contact_edit_one">
<action
android:id="@+id/action_contactEditOne_to_contactsList"
app:destination="@id/contactsList" />
</fragment>
</navigation>
<fragment
android:id="@+id/accountAboutUs"
android:name="com.example.techcrea.Fragments.Account.AccountAboutUs"
android:label="fragment_account_about_us"
tools:layout="@layout/fragment_account_about_us" />
<fragment
android:id="@+id/accountChangePassword"
android:name="com.example.techcrea.Fragments.Account.AccountChangePassword"
android:label="fragment_account_change_password"
tools:layout="@layout/fragment_account_change_password" />
<fragment
android:id="@+id/accountEditInformations"
android:name="com.example.techcrea.Fragments.Account.AccountEditInformations"
android:label="fragment_account_edit_informations"
tools:layout="@layout/fragment_account_edit_informations" >
<action
android:id="@+id/action_accountEditInformations_to_mnuAccount"
app:destination="@id/mnuAccount" />
</fragment>
</navigation>
</navigation>
这是我称之为的地方,其他所有链接都工作正常(直接链接)。 即使 Account 在嵌套图表中,底部导航视图也可以正常工作。
package com.example.techcrea.Fragments.Home;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import com.example.techcrea.Model.User;
import com.example.techcrea.R;
import com.example.techcrea.Services.DataStorage;
/**
* A simple @link Fragment subclass.
*/
public class Home extends Fragment implements View.OnClickListener
//VIEW ATTRIBUTES ##############################################################################
/**
* The text that displays our username
*/
private TextView displayUserName;
/**
* The horizontal dividers
*/
private View divider1, divider2, divider3, divider4, divider5;
/**
* The small icons left of each row
*/
private ImageView iconBills, iconAccount, iconContracts,iconContacts;
/**
* The pics for each type of contract
*/
private ImageView imgFiber, imgPhone, imgOther;
/**
* Clickable lines
*/
private LinearLayout accountRow, billRow, contractsRow, contactsRow, helpBlock;
//DATA ATTRIBUTES ##############################################################################
private User user;
/**
* Fragment sole constructor
*/
public Home()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
//Here we set the text that displays the user name
View view = inflater.inflate(R.layout.fragment_home, container, false);
user = DataStorage.getInstance().getUser().getValue();
loadViewComponents(view);
displayUserName.setText(user.getUserName());
return view;
/**
* Binds the design components to variables
*/
public void loadViewComponents(View view)
displayUserName = view.findViewById(R.id.txtWelcomeUser);
divider1 = view.findViewById(R.id.dvd1);
divider2 = view.findViewById(R.id.dvd2);
divider3 = view.findViewById(R.id.dvd3);
divider4 = view.findViewById(R.id.dvd4);
divider5 = view.findViewById(R.id.dvd5);
iconBills = view.findViewById(R.id.iconBills);
iconAccount = view.findViewById(R.id.iconAccount);
iconContracts = view.findViewById(R.id.iconContracts);
iconContacts = view.findViewById(R.id.iconContacts);
imgFiber = view.findViewById(R.id.img_fiber);
imgPhone = view.findViewById(R.id.img_phone);
imgOther = view.findViewById(R.id.img_other);
accountRow = view.findViewById(R.id.row_account);
billRow = view.findViewById(R.id.row_bills);
contractsRow = view.findViewById(R.id.row_contracts);
contactsRow = view.findViewById(R.id.row_contact);
helpBlock = view.findViewById(R.id.row_help);
setListeners();
/**
* The onClick Listeners for the menu rows.
*/
public void setListeners()
accountRow.setOnClickListener(this);
billRow.setOnClickListener(this);
helpBlock.setOnClickListener(this);
contactsRow.setOnClickListener(this);
contractsRow.setOnClickListener(this);
/**
* this method
* @param v the source of the click
*/
@Override
public void onClick(View v)
if (v.equals(accountRow))
Navigation.findNavController(getView()).navigate(R.id.action_mnuHome_to_accountMain);
else if (v.equals(billRow))
Navigation.findNavController(getView()).navigate(R.id.action_mnuHome_to_mnuBills);
else if (v.equals(helpBlock))
Navigation.findNavController(getView()).navigate(R.id.action_mnuHome_to_mnuHelp);
else if (v.equals(contactsRow))
**Navigation.findNavController(getView()).navigate(R.id.action_global_contactsList4);**
else if (v.equals(contractsRow))
Navigation.findNavController(getView()).navigate(R.id.action_mnuHome_to_mnuContracts);
这是我设置导航控制器的主要活动:
/**
* This is the "home" activity and it contains all the fragments we will navigate in.
* This activity is called by the LoginActivity.
* @author Caroline
*/
public class HomeActivity extends AppCompatActivity
private BottomNavigationView bView;
/**
* Navigation controller
*/
private NavController navController;
@Override
protected void onCreate(Bundle savedInstanceState)
//Android default code, do not touch _______________________________________________________
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//__________________________________________________________________________________________
//Toolbar instanciation
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setBackgroundColor(getResources().getColor(R.color.toolbar_color));
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
//Bottom Navigation : selected item by default at activity launch
bView = findViewById(R.id.bottom_navigation); //Bottom botnavigation bar
bView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED); //This line is to
force the display of texts under nav icons. Otherwise, only the active icon has its label
visible.
//Making it reactive to in-fragment links
//We add the fragment container layout to our botnavigation controller so it observes
botnavigation.
// To change the way the navController interacts with layout, please see
Res>Navigation>navgraph_bottomview.xml
navController = Navigation.findNavController(this,R.id.fragmentContainer);
NavigationUI.setupWithNavController(bView, navController);
NavigationUI.setupActionBarWithNavController(this,navController);
请原谅我奇怪的英语和我的代码错误我是法国人,仍然是学生,目前正在实习。
【问题讨论】:
您找到解决方案了吗?我遇到了同样的错误。 不,我没有,所以我从A删除了对联系人列表的访问,现在我只能从B访问它。没有更多问题,但也没有解决方案:'( 【参考方案1】:您需要include
A 导航图中的 C 图表。 IE。创建一个单独的导航图并像这样包含它:
<include app:graph="@navigation/mnuHelp_graph" />
【讨论】:
我会尝试,当我从假期回家时,C 图将不再链接/弹回/B 图当从 A 调用时,会吗?以上是关于此 NavController 不知道导航 [destination_name]的主要内容,如果未能解决你的问题,请参考以下文章
使用 NavController 从一个活动导航到另一个活动
IllegalStateException:链接没有设置 NavController