标签: android-safe-args

在导航组件中使用带有安全参数的可打包

我想在版本中将Parcelable与Navigation Components和Safe Args一起使用1.0.0-alpha07。尽管自从Alpha 03 Parcelable应该由Safe Args支持,但是我无法使用它们。使用它的唯一方法是app:argType="android.os.Parcelable"对我来说似乎不是正确的方法。

如果仅使用app:argType="Parcelable",则在生成的类中不会生成任何导入。因此,将Parcelable与Safe Args结合使用的正确方法是什么?

android parcelable android-architecture-components android-safe-args

5
推荐指数
1
解决办法
552
查看次数

使用Android导航组件中的navigationUp传递数据/捆绑

我找到了问题,但是代码中没有解决方案

我想在有backpress / manual back发生时获取数据。我navigateUp()过去经常回去。如何将数据传递到上一个片段?navigateUp()没有任何工具可以将数据传递到上一个片段。甚至我也没有找到使用Safe Args的解决方案。它正在转发数据。我想在向后的Frad B-> Frag A中使用。

我的代码回到上一个片段

Navigation.findNavController(view).navigateUp()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我的问题是,如何获取上一个片段中的数据。我可以使用以下方法从Frag B导航至Frag A

android android-fragments android-architecture-navigation android-safe-args

4
推荐指数
3
解决办法
1341
查看次数

原因:androidx.navigation.safeargs只能与androidx项目一起使用

我遇到名为的导航组件的问题safeargs

我用classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.0.0'在构建gradle。

在中应用插件androidx.navigation.safeargs.kotlinapp/build.gradle,出现以下错误:

原因:androidx.navigation.safeargs只能与androidx项目一起使用

app / build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

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

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.test.navigationexample"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha4'
    implementation 'com.google.android.material:material:1.1.0-alpha05'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
}
Run Code Online (Sandbox Code Playgroud)

android kotlin android-architecture-components android-architecture-navigation android-safe-args

4
推荐指数
4
解决办法
1400
查看次数

如何使用安全参数传递“位图”作为参数

我想Bitmap使用安全参数将参数传递给另一个片段。argType传递位图的正确方法是什么?

<fragment
        android:id="@+id/nextFragment"
        android:name="com.example.----.NextFragment"
        android:label="fragment_next"
        tools:layout="@layout/fragment_next" >
        <argument
            android:name="image"
            app:argType="???"
            android:defaultValue="???" />
</fragment>
Run Code Online (Sandbox Code Playgroud)

android android-architecture-components android-jetpack android-architecture-navigation android-safe-args

4
推荐指数
1
解决办法
1792
查看次数

缺少必需的参数“名称”并且没有 android:defaultValue

我的代码有问题,我试图在片段之间传递参数,但java.lang.IllegalArgumentException: Required argument "name" is missing and does not have an android:defaultValue在运行时出现异常 。我仍然是初学者,所以我无法真正说出问题的来源。

class ListNotesFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val binding = DataBindingUtil.inflate<FragmentListNotesBinding>(inflater, R.layout.fragment_list_notes, container, false)

        val application = requireNotNull(this.activity).application

        val args = ListNotesFragmentArgs.fromBundle(arguments!!).name

        val dataSource = NoteDatabase.getInstance(application).noteDatabaseDao

        val viewModelFactory = ListNoteViewModelFactory(args, dataSource)

        val listNoteViewModel = ViewModelProviders.of(this, viewModelFactory).get(ListNoteViewModel::class.java)

        binding.listNoteViewModel = listNoteViewModel

        binding.lifecycleOwner = this

        binding.addButton.setOnClickListener{
            this.findNavController().navigate(R.id.action_listNotesFragment_to_detailNoteFragment)
        }

        listNoteViewModel.navigateToDetailNoteFragment.observe(viewLifecycleOwner, Observer{
            it?.let {
                this.findNavController().navigate(ListNotesFragmentDirections
                    .actionListNotesFragmentToDetailNoteFragment(it.noteId))

                listNoteViewModel.doneNavigating()
            }
        })

        return …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-architecture-navigation android-safe-args

4
推荐指数
1
解决办法
2285
查看次数

将 ArrayList 作为参数传递给 safeArgs

我试图ArrayList<String>在使用 safeArgs 时将 an作为参数传递。但是当我从可序列化和构建的列表中选择 ArrayList 时,它向我抛出这些异常

One type argument expected for class ArrayList<E : Any!>
One type argument expected. Use 'ArrayList<*>' if you don't want to pass type arguments
Run Code Online (Sandbox Code Playgroud)

我知道我必须为 ArrayList 设置数据类型,但在从导航图屏幕设置 safeArgs 时,我无法这样做,也没有选项。

在导航图 XML 中,代码生成为

<argument
            android:name="teamsToAdd"
            app:argType="java.util.ArrayList" />
Run Code Online (Sandbox Code Playgroud)

有没有办法在导航图的 XML 中声明 dataType?

android kotlin android-navigation android-safe-args android-navigation-graph

4
推荐指数
1
解决办法
3211
查看次数

如果默认值 null,如何使用 android 导航传递值

我正在使用带有安全参数的 Android 导航组件。我将参数设置为可以为空,并且默认值也为空。

问题是当我想传递任何值时会显示错误:

required: no arguments
found: Character
reason: actual and formal argument lists differ in length
Run Code Online (Sandbox Code Playgroud)

我的片段 XML 代码:

<fragment
        android:id="@+id/bookListFragment"
        android:name="com.example.bookstory.UI.Fragments.BookListFragment"
        android:label="fragment_book_list"
        tools:layout="@layout/fragment_book_list">
            <argument
            android:name="character"
            app:argType="com.example.bookstory.DAO.Character"
            app:nullable="true"
            android:defaultValue="@null" />
</fragment>
Run Code Online (Sandbox Code Playgroud)

我的行动:

      <action
            android:id="@+id/action_bookDescriptionFragment_to_bookListFragment"
            app:destination="@id/bookListFragment"
            app:popUpTo="@id/bookListFragment"
            app:popUpToInclusive="true" />
Run Code Online (Sandbox Code Playgroud)

我不明白问题是什么 - 当我删除默认值时就可以了。

navigation android android-safe-args

4
推荐指数
1
解决办法
3300
查看次数

FragmentDirections 不是由 Safe Args 插件生成的

当我尝试使用 Safe Args 插件从一个片段跳转到另一个片段时,编译器显示“未解析的引用:VideoFragmentDirections”。我已经设置了安全参数的类路径和依赖项,并且 VideoFragmentArgs 已正确生成。kotlin_version = '1.3.50' navigation-safe-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>
Run Code Online (Sandbox Code Playgroud)

在 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 …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-safe-args

3
推荐指数
2
解决办法
1万
查看次数

当我将相同的片段添加到第二个导航图时,无法识别操作类

我需要将相同的片段添加到不同的导航图。这是第一张图( mobile_navigation)的代码片段

<fragment
    android:id="@+id/nav_contactanos"
    android:name="com.engie.mexico.micuenta.Fragments.FragmentContactanosCliente"
    tools:layout="@layout/fragment_contactanos_cliente" >
    <action
        android:id="@+id/action_nav_contactanos_to_nav_aviso_privacidad"
        app:destination="@id/nav_aviso_privacidad" />
    <action
        android:id="@+id/action_nav_contactanos_to_nav_terminos_condiciones"
        app:destination="@id/nav_terminos_condiciones" />
    <action
        android:id="@+id/action_nav_contactanos_to_nav_mensaje_usuario"
        app:destination="@id/nav_mensaje_usuario" />
</fragment>
Run Code Online (Sandbox Code Playgroud)

第二个导航图中的第二个代码片段 ( navigation_initial )

    <fragment
    android:id="@+id/nav_contactanos_cliente"
    android:name="com.engie.mexico.micuenta.Fragments.FragmentContactanosCliente"
    tools:layout="@layout/fragment_contactanos_cliente" >
    <action
        android:id="@+id/action_nav_contactanos_cliente_to_nav_mensaje_usuario"
        app:destination="@id/nav_mensaje_usuario" />
    <action
        android:id="@+id/action_nav_contactanos_cliente_to_nav_terminos_condiciones"
        app:destination="@id/nav_terminos_condiciones" />
    <action
        android:id="@+id/action_nav_contactanos_cliente_to_nav_aviso_privacidad"
        app:destination="@id/nav_aviso_privacidad" />
</fragment>
Run Code Online (Sandbox Code Playgroud)

当我尝试 MakeProject 时,构建输出向我显示错误:

C:\Android\MiCuenta\app\src\main\java\com\engie\mexico\micuenta\Fragments\FragmentContactanosCliente.java:478: error: cannot find symbol
            FragmentContactanosClienteDirections.ActionNavContactanosToNavMensajeUsuario action = symbol:   class ActionNavContactanosToNavMensajeUsuario
Run Code Online (Sandbox Code Playgroud)

位置:类 FragmentContactanosClienteDirections

但是当我评论第二段代码(navigation_initial)时,问题就消失了。问题是我需要第二段代码,因为我需要调用片段并与其共享捆绑包,与其他信息并显示不同的内容......如何实现这一点?

我也分享一下图表的设计:

移动导航

mobile_navigation.xml(设计)

导航_初始

navigation_initial.xml(设计)

navigation android android-safe-args android-navigation-graph

3
推荐指数
1
解决办法
2316
查看次数

在我的情况下,如何修复“无法从当前目的地找到导航操作/目的地”错误?

我在用bottom navigation

在切换的屏幕中,还有打开dialog fragment.

我也为此使用了导航。

这是因为,一旦dialog完成,data就必须传递到打开对话框的屏幕。

我用的safe args是这个。

但我遇到了与标题相同的错误。

我知道错误在哪里,但我不知道为什么会发生。

根据Stack Overflow上的搜索,有人遇到了和我一样的问题,但似乎并不是一个确切的解决方案,因为情况不同。

导航图

<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/calendar">
    <fragment
        android:id="@+id/calendar"
        android:name="com.example.writeweight.fragment.CalendarFragment"
        android:label="fragment_calendar"
        tools:layout="@layout/fragment_calendar" >
    </fragment>
    <fragment
        android:id="@+id/list"
        android:name="com.example.writeweight.fragment.WorkoutListFragment"
        android:label="fragment_workout_list"
        tools:layout="@layout/fragment_workout_list" />
    <fragment
        android:id="@+id/write"
        android:name="com.example.writeweight.fragment.WritingRoutineFragment"
        android:label="WritingRoutineFragment"
        tools:layout="@layout/fragment_writing_routine">
        <action
            android:id="@+id/action_write_to_bodyPartDialog"
            app:destination="@id/bodyPartDialog" />
        <argument
            android:name="title"
            app:argType="string"
            android:defaultValue="Write" />
    </fragment>
    <dialog
        android:id="@+id/bodyPartDialog"
        android:name="com.example.writeweight.fragment.BodyPartDialogFragment"
        android:label="BodyPartDialogFragment"
        tools:layout="@layout/fragment_body_part_dialog">
        <action
            android:id="@+id/action_bodyPartDialog_to_write"
            app:destination="@id/write">
        </action>
    </dialog>
</navigation>
Run Code Online (Sandbox Code Playgroud)

主要活动

class MainActivity : AppCompatActivity() {
    private lateinit var bottomNav: BottomNavigationView
    private …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-navigation android-safe-args

3
推荐指数
1
解决办法
6977
查看次数