我已经多次看到这个问题,但大多数人需要从C返回A,而我的情况相反。
这是我的导航图的一部分:
HOME -> 嵌套图账户 -> 嵌套图账户内的联系人。
我想做的是从HOME跳转到Contacts。我尝试了在xml navgraph中的每个片段中使用全局操作,但每次都会收到相同的错误:
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)
当我使用“navigate”从“simple”操作的帐户访问“联系人”时,它能正常工作。只有从主页的“shortcut”不起作用。
这是我的导航图,我把全局操作放在它的顶部,但是我尝试把它放在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);
}
}
请谅解我的奇怪英语和代码错误,我是法国人,还是学生,目前正在实习。