此 NavController 不知道操作
Action is unknown to this NavController
我以两种不同的方式提供片段之间的数据传输,第一种是正常工作,而安全数据类型会导致 运行 时崩溃。我正在使用 Android 文档:
https://developer.android.com/topic/libraries/architecture/navigation/navigation-implementing#Safe-args
不明白我该如何解决这个问题。感谢您的帮助
07-13 11:40:07.986 8119-8119/com.flexdecision.ak_lex.navigationsimple E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.flexdecision.ak_lex.navigationsimple, PID: 8119
java.lang.IllegalArgumentException: navigation destination com.flexdecision.ak_lex.navigationsimple:id/transferAction is unknown to this NavController
at androidx.navigation.NavController.navigate(NavController.java:669)
at androidx.navigation.NavController.navigate(NavController.java:628)
at androidx.navigation.NavController.navigate(NavController.java:690)
at com.flexdecision.ak_lex.navigationsimple.InitialFragment.makeTransfer(InitialFragment.java:58)
at com.flexdecision.ak_lex.navigationsimple.InitialFragment.lambda$onViewCreated[=10=](InitialFragment.java:47)
at com.flexdecision.ak_lex.navigationsimple.-$$Lambda$InitialFragment$shEoLbIe0sVhbTcJ2Al_FvBuU7g.onClick(lambda)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
导航图:
<?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/nav_graph"
app:startDestination="@id/initialFragment">
<fragment
android:id="@+id/initialFragment"
android:name="com.flexdecision.ak_lex.navigationsimple.InitialFragment"
android:label="fragment_initial"
tools:layout="@layout/fragment_initial">
<action
android:id="@+id/transferAction"
app:destination="@+id/nextFragment" />
</fragment>
<fragment
android:id="@+id/nextFragment"
android:name="com.flexdecision.ak_lex.navigationsimple.NextFragment"
android:label="fragment_next"
tools:layout="@layout/fragment_next" >
<argument
android:name="firstName"
android:defaultValue="none"
app:type="string" />
<argument
android:name="lastName"
android:defaultValue="none"
app:type="string" />
</fragment>
</navigation>
Activity布局:
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
</android.support.constraint.ConstraintLayout>
发件人:
import androidx.navigation.Navigation;
public class InitialFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_initial, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button transactionBtn = view.findViewById(R.id.transaction);
transactionBtn.setOnClickListener(v -> makeTransfer(v));
}
private void makeTransfer(View view) {
Bundle bundle = new Bundle();
bundle.putString("name", "Aleksey");
Navigation.findNavController(view).navigate(R.id.transferAction, bundle);
//Type safe passing data
InitialFragmentDirections.TransferAction action = InitialFragmentDirections.transferAction();
action.setLastName("Petrov");
Navigation.findNavController(view).navigate(action);
}
}
接收者:
public class NextFragment extends Fragment {
private static final String ARG_PARAM1 = "name";
private String mParam1;
private TextView firstName;
public NextFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
Log.d("Next", "Param: " + mParam1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_next, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
firstName = view.findViewById(R.id.firstName);
TextView lastName = view.findViewById(R.id.lastNameTV);
firstName.setText(mParam1);
String ln = NextFragmentArgs.fromBundle(getArguments()).getLastName();
lastName.setText(ln);
}
}
补充:
apply plugin: "androidx.navigation.safeargs"
dependencies {
def nav_version = "1.0.0-alpha03"
implementation "android.arch.navigation:navigation-fragment:$nav_version"
implementation "android.arch.navigation:navigation-ui:$nav_version"
}
您给 'Navigation.findNavController(view).navigate' 打了两次电话:
private void makeTransfer(View view) {
Bundle bundle = new Bundle();
bundle.putString("name", "Aleksey");
Navigation.findNavController(view).navigate(R.id.transferAction, bundle);
//Type safe passing data
InitialFragmentDirections.TransferAction action = InitialFragmentDirections.transferAction();
action.setLastName("Petrov");
Navigation.findNavController(view).navigate(action);
}
第一次使用 bundle,第二次使用 safe args,但是在第一次调用之后,您的目的地已经更改为 'nextFragment',而当您调用第二次 'navigate' 时,'NavController' 正在寻找'transferAction' 'nextFragment' 内的操作并抛出异常。
1.Add 波纹管 gradle 文件
中的依赖关系
def nav_version = "2.2.0"
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
2.Add 低于您的 Project/Top 级别的插件 Gradle
def nav_version = "2.2.0"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
3.Rebuild 你的项目
在我的例子中,我试图从片段 A 导航到片段 B,但片段 A 不是通过导航控制器导航到的(也就是导航图中不存在),它是我明确指出的片段从 FragmentStateAdater 初始化(绑定到 ViewPager)。解决方案是为片段 B 创建一个全局操作,而不是从片段 A 到片段 B
我以两种不同的方式提供片段之间的数据传输,第一种是正常工作,而安全数据类型会导致 运行 时崩溃。我正在使用 Android 文档: https://developer.android.com/topic/libraries/architecture/navigation/navigation-implementing#Safe-args
不明白我该如何解决这个问题。感谢您的帮助
07-13 11:40:07.986 8119-8119/com.flexdecision.ak_lex.navigationsimple E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.flexdecision.ak_lex.navigationsimple, PID: 8119
java.lang.IllegalArgumentException: navigation destination com.flexdecision.ak_lex.navigationsimple:id/transferAction is unknown to this NavController
at androidx.navigation.NavController.navigate(NavController.java:669)
at androidx.navigation.NavController.navigate(NavController.java:628)
at androidx.navigation.NavController.navigate(NavController.java:690)
at com.flexdecision.ak_lex.navigationsimple.InitialFragment.makeTransfer(InitialFragment.java:58)
at com.flexdecision.ak_lex.navigationsimple.InitialFragment.lambda$onViewCreated[=10=](InitialFragment.java:47)
at com.flexdecision.ak_lex.navigationsimple.-$$Lambda$InitialFragment$shEoLbIe0sVhbTcJ2Al_FvBuU7g.onClick(lambda)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
导航图:
<?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/nav_graph"
app:startDestination="@id/initialFragment">
<fragment
android:id="@+id/initialFragment"
android:name="com.flexdecision.ak_lex.navigationsimple.InitialFragment"
android:label="fragment_initial"
tools:layout="@layout/fragment_initial">
<action
android:id="@+id/transferAction"
app:destination="@+id/nextFragment" />
</fragment>
<fragment
android:id="@+id/nextFragment"
android:name="com.flexdecision.ak_lex.navigationsimple.NextFragment"
android:label="fragment_next"
tools:layout="@layout/fragment_next" >
<argument
android:name="firstName"
android:defaultValue="none"
app:type="string" />
<argument
android:name="lastName"
android:defaultValue="none"
app:type="string" />
</fragment>
</navigation>
Activity布局:
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
</android.support.constraint.ConstraintLayout>
发件人:
import androidx.navigation.Navigation;
public class InitialFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_initial, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button transactionBtn = view.findViewById(R.id.transaction);
transactionBtn.setOnClickListener(v -> makeTransfer(v));
}
private void makeTransfer(View view) {
Bundle bundle = new Bundle();
bundle.putString("name", "Aleksey");
Navigation.findNavController(view).navigate(R.id.transferAction, bundle);
//Type safe passing data
InitialFragmentDirections.TransferAction action = InitialFragmentDirections.transferAction();
action.setLastName("Petrov");
Navigation.findNavController(view).navigate(action);
}
}
接收者:
public class NextFragment extends Fragment {
private static final String ARG_PARAM1 = "name";
private String mParam1;
private TextView firstName;
public NextFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
Log.d("Next", "Param: " + mParam1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_next, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
firstName = view.findViewById(R.id.firstName);
TextView lastName = view.findViewById(R.id.lastNameTV);
firstName.setText(mParam1);
String ln = NextFragmentArgs.fromBundle(getArguments()).getLastName();
lastName.setText(ln);
}
}
补充:
apply plugin: "androidx.navigation.safeargs"
dependencies {
def nav_version = "1.0.0-alpha03"
implementation "android.arch.navigation:navigation-fragment:$nav_version"
implementation "android.arch.navigation:navigation-ui:$nav_version"
}
您给 'Navigation.findNavController(view).navigate' 打了两次电话:
private void makeTransfer(View view) {
Bundle bundle = new Bundle();
bundle.putString("name", "Aleksey");
Navigation.findNavController(view).navigate(R.id.transferAction, bundle);
//Type safe passing data
InitialFragmentDirections.TransferAction action = InitialFragmentDirections.transferAction();
action.setLastName("Petrov");
Navigation.findNavController(view).navigate(action);
}
第一次使用 bundle,第二次使用 safe args,但是在第一次调用之后,您的目的地已经更改为 'nextFragment',而当您调用第二次 'navigate' 时,'NavController' 正在寻找'transferAction' 'nextFragment' 内的操作并抛出异常。
1.Add 波纹管 gradle 文件
中的依赖关系def nav_version = "2.2.0"
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
2.Add 低于您的 Project/Top 级别的插件 Gradle
def nav_version = "2.2.0"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
3.Rebuild 你的项目
在我的例子中,我试图从片段 A 导航到片段 B,但片段 A 不是通过导航控制器导航到的(也就是导航图中不存在),它是我明确指出的片段从 FragmentStateAdater 初始化(绑定到 ViewPager)。解决方案是为片段 B 创建一个全局操作,而不是从片段 A 到片段 B