【问题标题】:How to solve isssue of Required: androidx.fragment.app.Fragment in a read only fragment如何解决必需的问题:只读片段中的androidx.fragment.app.Fragment
【发布时间】:2020-01-14 21:39:32
【问题描述】:

我做了什么:

  • 我正在使用 this 库并通过 gradle 导入。导入时没有问题。
  • 我正在尝试使用它的片段之一。

问题:

尝试解决:

  • 尝试建议重构,但由于所提供片段的“只读”属性无法完成,最终收到消息。 ()

有什么办法可以摆脱这个错误吗?为什么会这样?

~谢谢~

Gradle 文件:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.test3"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
repositories {
    jcenter()
    maven {
        url  "http://dl.bintray.com/shimmerengineering/Shimmer"
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'ShimmerAndroidInstrumentDriver:ShimmerAndroidInstrumentDriver:AA-227-1test'
}

gradle.properties 文件:

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true

【问题讨论】:

  • 如果您的项目不使用 androidx,请从菜单栏中尝试Refactor > Migrate to AndroidX
  • 谢谢@MohammedAlaa,我试过这个并在下面的讨论中回复。似乎工作项目已经使用 android,但导入的库不是,通过尝试 Refactor > Migrate to AndroidX 仅影响项目而不影响库。

标签: android api android-studio android-fragments androidx


【解决方案1】:

您正在使用的库尚未迁移到 AndroidX。因此,您必须在您的项目中使用enable Jetifier - 这会自动将非 AndroidX 项目转换为 AndroidX 项目,让您可以像使用 SignalsToPlotFragment 一样 使用 androidx.fragment.app.Fragment

您希望将以下行添加到您的gradle.properties file,如果该文件尚不存在,则创建该文件:

android.useAndroidX=true
android.enableJetifier=true

【讨论】:

  • 既然有源代码,重构库也是一种选择。
  • @MartinZeitler - 如果你能让他们同意重构并发布新版本,当然可以。 Jetifier 的全部意义在于您无需等待库来执行此操作。
  • 每当我打算使用某个东西并且它在 GitHub 上时,我都会重构然后 PR;确定这取决于所涉及的努力,是否真的值得 - 或者 Jetifier 是否更有意义。
  • @MartinZeitler - 是的,如果你有时间,没有理由不两者都做(使用 Jetifier 作为权宜之计)。
  • 我只是在一个项目被放弃的时候这样做——这样人们至少可以找到 PR。
【解决方案2】:

从代码here看,他们好像还没有更新到androidx,还在使用v4支持库android.support.v4.app.Fragment

由于你的 gradle 属性中有 android.enableJetifier=true,你应该可以在你的 gradle 文件中添加片段库:

implementation 'androidx.fragment:fragment:1.1.0'

这将是您班级中的正确导入:

import androidx.fragment.app.Fragment;

如果这不起作用,请尝试也添加(到 gradle):implementation 'com.android.support:support-v4:25.3.1'here 一样,并在您的课程中使用 import android.support.v4.app.Fragment;,直到它们也迁移到 API 29 和 androidx。

【讨论】:

  • 谢谢...,我将 {implementation 'androidx.legacy:legacy-support-v4:1.0.0'} 添加到 gradle 中,因为它表明 'compile' 已过时。更新 gradle 后,它建议做 Refactor > Migrate to AndroidX,我做到了。然后红色下划线消失了。在主要活动中,我评论了 {//import androidx.fragment.app.Fragment;} 并按照您的建议添加 {import android.support.v4.app.Fragment;}。但似乎没有运气......这里的“片段”这个词是红色的。
  • androidx.legacy:legacy-support-v4com.android.support:support-v4 不同,但没关系 - 请参阅更新的答案
  • 感谢您的指导。我尝试了这两个选项,1 - 没用。 2 - 更新 gradle 时,它​​与此评论同步//noinspection GradleCompatible,GradleCompatible implementation 'com.android.support:support-v4:25.3.1',在添加 import android.support.v4.app.Fragment; 后观察到“片段”的相同红色下划线,表示不工作。好吧..也许我可以尝试在 andoridx 中创建片段类
猜你喜欢
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
  • 2019-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多