Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android DataBindingUtil+NavHostFragment反向导航不工作_Android_Android Databinding_Android Jetpack_Android Jetpack Navigation - Fatal编程技术网

Android DataBindingUtil+NavHostFragment反向导航不工作

Android DataBindingUtil+NavHostFragment反向导航不工作,android,android-databinding,android-jetpack,android-jetpack-navigation,Android,Android Databinding,Android Jetpack,Android Jetpack Navigation,之前关于NAVOSTFragment在背压上不工作的问题。这个问题没有得到任何人的回答,因此花了几天的时间,我终于设法解决了这个问题。(我只是添加了前面问题中缺少的新代码,并在这里更改了代码,以减少问题长度) 导航图更改为以下内容: <?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="http://schemas.android.com/apk/res/android" xm

之前关于NAVOSTFragment在背压上不工作的问题。这个问题没有得到任何人的回答,因此花了几天的时间,我终于设法解决了这个问题。(我只是添加了前面问题中缺少的新代码,并在这里更改了代码,以减少问题长度)

导航图更改为以下内容:

<?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/launch_navigation_graph"
            app:startDestination="@id/splashFragment">

    <fragment android:id="@+id/splashFragment" android:name="com.myapp.android.SplashFragment"
              android:label="fragment_splash" tools:layout="@layout/fragment_splash">
        <action android:id="@+id/action_splashFragment_to_fragment1"
                app:destination="@id/fragment1"
        app:popUpTo="@+id/splashFragment"
                app:popUpToInclusive="true"/>
    </fragment>
    <fragment android:id="@+id/fragment1"
              android:name="com.myapp.android.Fragment1"
              android:label="fragment1" tools:layout="@layout/fragment_register_msisdn">
        <action android:id="@+id/action_fragment1_to_fragment2"
                app:destination="@id/fragment2" app:popUpTo="@+id/fragment1"
                app:popUpToInclusive="false"/>
    </fragment>
    <fragment android:id="@+id/fragment2"
              android:name="com.myapp.android.Fragment2"
              android:label="fragment_fragment2" tools:layout="@layout/fragment_fragment2"/>
</navigation>
上面带有导航图的代码现在不会崩溃,但“后退”按钮根本不起作用。它不会弹出碎片,只需在第二次后按退出即可

现在我发现使用
DataBindingUtil.setContentView
膨胀UI是我的代码无法工作的原因。我一步一步地创建另一个示例项目,直到我知道它何时停止工作。我使用DataBindingUtil绑定到OnCreateView中的视图,使用数据绑定使示例项目工作,如下所示。我删除oncreate覆盖

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val rootView = inflater.inflate(R.layout.fragment1, container, false)
    val binding = DataBindingUtil.bind<Fragment1Binding>(rootView)
    binding?.lifecycleOwner = viewLifecycleOwner
    val fragment1ViewModel = ViewModelProviders.of(this).get(Fragment1ViewModel::class.java)
    binding.viewModel = fragment1ViewModel
    return rootView
}
我在谷歌上搜索了一个例外,但在这个特殊的环境中,我发现没有什么有用的东西可以解决我的问题。请帮忙

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val rootView = inflater.inflate(R.layout.fragment1, container, false)
    val binding = DataBindingUtil.bind<Fragment1Binding>(rootView)
    binding?.lifecycleOwner = viewLifecycleOwner
    val fragment1ViewModel = ViewModelProviders.of(this).get(Fragment1ViewModel::class.java)
    binding.viewModel = fragment1ViewModel
    return rootView
}
2019-04-30 14:00:47.043 18087-18087/com.selfcare.safaricom E/InputEventSender: Exception dispatching finished signal.
2019-04-30 14:00:47.043 18087-18087/com.selfcare.safaricom E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2019-04-30 14:00:47.047 18087-18087/com.selfcare.safaricom E/MessageQueue-JNI: java.lang.IllegalArgumentException: navigation destination com.selfcare.safaricom:id/action_fragment1_to_fragment2 is unknown to this NavController
        at androidx.navigation.NavController.navigate(NavController.java:803)
        at androidx.navigation.NavController.navigate(NavController.java:744)
        at androidx.navigation.NavController.navigate(NavController.java:730)
        at androidx.navigation.NavController.navigate(NavController.java:718)
        at com.myapp.android.Fragment2.handleLaunchStatus(Fragment2.kt:53)
        at com.myapp.android.Fragment2.access$handleLaunchStatus(Fragment2.kt:18)
        at com.myapp.android.Fragment2$attachLaunchObserver$1.onChanged(Fragment2.kt:46)
        at com.myapp.android.Fragment2$attachLaunchObserver$1.onChanged(Fragment2.kt:18)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:113)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
        at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:424)
        at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:376)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
        at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:188)
        at androidx.lifecycle.LiveData.observe(LiveData.java:185)
        at com.myapp.android.Fragment2.attachLaunchObserver(Fragment2.kt:45)
        at com.myapp.android.Fragment2.onViewCreated(Fragment2.kt:37)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:895)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2092)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1822)
        at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:298)
        at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:241)
        at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:288)
        at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:241)
        at androidx.fragment.app.FragmentActivity$1.handleOnBackPressed(FragmentActivity.java:144)
        at androidx.activity.OnBackPressedDispatcher.onBackPressed(OnBackPressedDispatcher.java:136)
        at androidx.activity.ComponentActivity.onBackPressed(ComponentActivity.java:283)
        at android.app.Activity.onKeyUp(Activity.java:3083)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2716)
        at android.app.Activity.dispatchKeyEvent(Activity.java:3366)
        at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:80)
        at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
        at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:98)
        at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:558)
        at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
        at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2736)
        at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:342)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5037)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4905)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4426)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4479)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4445)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4585)
        at android.view.ViewRootImpl$InputStage.apply(Vie