【问题标题】:FragmentDirections was not generate by Safe Args pluginFragmentDirections 不是由 Safe Args 插件生成的
【发布时间】:2019-11-25 15:24:22
【问题描述】:

当我尝试使用 Safe Args 插件从一个片段跳转到另一个片段时,编译器显示“未解析的引用:VideoFragmentDirections”。 我已经为安全参数设置了类路径和依赖项,并且 VideoFragmentArgs 已正确生成。 kotlin_version = '1.3.50' 导航-安全-args-gradle-plugin:2.1.0 在我的xml中

<fragment
        android:id="@+id/nav_home"
        android:name="example.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_nav_home_to_videoFragment"
            app:destination="@id/videoFragment"
            app:popUpTo="@+id/nav_home"/>
    </fragment>
<fragment
        android:id="@+id/videoFragment"
        android:name="example.ui.videoui.VideoFragment"
        android:label="VideoFragment" >
        <argument
            android:name="id"
            app:argType="long"
            android:defaultValue="0L" />
    </fragment>

在 HomeFragment.ky 中

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val videoViewModel = ViewModelProviders
            .of(this)
            .get(HomeViewModel::class.java)
        val adapter = video_list.adapter!! as PlaylistAdapter
        videoViewModel.videos.observe(this, Observer<List<Video>> {
            adapter.submitList(it)
            adapter.onItemClickListener = View.OnClickListener { v ->

                val viewHolder = v.tag as RecyclerView.ViewHolder
                val position = viewHolder.adapterPosition
                val id = it[position].id
                val action = VideoFragmentDirections.actionhHomeToVideo(id)
                view.findNavController().navigate(action)
            }
        })
}

另外,我曾尝试清理和重建项目,但仍然无法正常工作。

【问题讨论】:

    标签: android kotlin android-safe-args


    【解决方案1】:

    试试

    apply plugin: "androidx.navigation.safeargs.kotlin"
    

    在应用程序 build.gradle 中

    【讨论】:

      【解决方案2】:
      // Project level build.gradle 
      buildscript {
          repositories {
              google()
          }
          dependencies {
              classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version") //add here.
          }
      }
      
      
      // Module level build.gradle.
      plugins {
          id 'androidx.navigation.safeargs'// add here.
      }
      

      祝你天天快乐?

      【讨论】:

      • 这对我有用,感谢您的回答
      【解决方案3】:

      确保在添加新依赖项后重建项目。如果需要,也可以更新您的项目 gradle 文件中的版本。

      这很好用。

      【讨论】:

        猜你喜欢
        • 2021-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-14
        • 2017-02-02
        相关资源
        最近更新 更多