当前位置: 首页 > news >正文

错误: 程序包androidx.fragment.app不存在 import android

错误: 程序包androidx.fragment.app不存在 import androidx.fragment.app.FragmentActivity; 这个是什么错?dependencies { //implementation fileTree(dir: 'libs', include: ['*.jar']) implementation project(path: ':libscan') //noinspection GradleCompatible implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.alibaba:fastjson:1.2.68' implementation 'androidx.test:monitor:1.4.0' implementation 'androidx.test:core:1.4.0' // implementation 'androidx.fragment:fragment:1.6.2' // 或最新版本 //implementation files('libs\\android.jar') compileOnly files('libs\\android.jar') implementation files('libs\\juniversalchardet-1.0.3.jar') implementation files('libs/lfilepickerlibrary-debug.aar') // implementation 'androidx.camera:camera-lifecycle:1.0.0' // implementation 'androidx.camera:camera-core:1.0.0' // implementation 'androidx.camera:camera-camera2:1.0.0' // implementation 'androidx.camera:camera-lifecycle:1.0.0' testImplementation 'junit:junit:4.12' //noinspection GradleCompatible implementation 'com.android.support:design:28.0.0' // implementation 'com.github.leonHua:LFilePicker:1.8.0' // implementation 'com.leon:lfilepickerlibrary:1.7.9' // implementation 'com.leon:lfilepickerlibrary:1.8.0' implementation 'androidx.test:core:1.4.0' implementation 'androidx.test:core-ktx:1.4.0' // androidTestImplementation 'androidx.test:core:1.4.0' // implementation 'net.sourceforge.streamsupport:streamsupport:1.7.1' } def static releaseTime() { return new Date().format("yyyyMMdd", TimeZone.getTimeZone("GMT+8")) } gradle文件是这样的会报错。那么 gradle文件这样就不报错dependencies { //implementation 'com.android.support:support-v4:24.2.1' // implementation 'com.android.support:support-v4:28.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.alibaba:fastjson:1.2.68' //implementation 'androidx.lifecycle:lifecycle-common-jvm:2.8.7' compileOnly files('libs\\framework.jar') implementation files('libs\\juniversalchardet-1.0.3.jar') // implementation 'androidx.core:core:1.6.0' // implementation project(':sdklibrary') // Java language implementation }?

你这个错误:

错误: 程序包androidx.fragment.app不存在
import androidx.fragment.app.FragmentActivity;

说明:你的项目中没有引入 androidx.fragment 所属库,但你又用了 FragmentActivity


✅ 问题分析

你当前 build.gradle 文件中,混用了 android.supportandroidx,导致编译环境不一致。

  • androidx.fragment.app.FragmentActivity 是 AndroidX 体系的;

  • com.android.support:appcompat-v7:28.0.0 是老的 support 体系;

  • 两者不能混用,否则就会报找不到类。


✅ 正确做法(根据你想要的方案)


✅ 如果你要使用 AndroidX(推荐)

请使用如下 build.gradle 配置(清理掉所有 com.android.support:*):

android {compileSdkVersion 33defaultConfig {applicationId "your.package.name"minSdkVersion 21targetSdkVersion 33versionCode 1versionName "1.0"// 启用 AndroidXmultiDexEnabled true}buildFeatures {viewBinding true}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}// AndroidX 开关(仅旧项目升级时需要)buildProperties {android.useAndroidX=trueandroid.enableJetifier=true}
}dependencies {implementation 'androidx.appcompat:appcompat:1.3.1'implementation 'androidx.constraintlayout:constraintlayout:2.1.4'implementation 'androidx.legacy:legacy-support-v4:1.0.0'implementation 'com.google.android.material:material:1.4.0'implementation 'androidx.fragment:fragment:1.6.2' // ✅ 引入 FragmentActivityimplementation 'com.alibaba:fastjson:1.2.68'compileOnly files('libs/framework.jar')implementation files('libs/juniversalchardet-1.0.3.jar')testImplementation 'junit:junit:4.13.2'
}

✅ 如果你仍想坚持使用 support库(不推荐)

  • 删掉所有 androidx.* 的依赖;

  • 保留:

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'

但此方式已经是旧架构(Google 早已弃用),不建议继续使用。


✅ 建议结论

你上面最后一个不报错的配置,其实是已经 完全迁移到 AndroidX 的方式

implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.fragment:fragment:1.6.2'

那你需要保证:

  1. 所有 Java 文件中只使用 androidx.* 类;

  2. 不要混用 com.android.support.*


🧹 额外建议:清理旧缓存和强制同步

执行以下操作确保稳定:

./gradlew clean
./gradlew build --refresh-dependencies

如你还需要 build.gradle 自动判断是否使用 support 还是 androidx、

http://www.lryc.cn/news/574110.html

相关文章:

  • 实现 el-table 中键盘方向键导航功能vue2+vue3(类似 Excel)
  • Flutter ListTile 徽章宽度自适应的真正原因与最佳实践
  • iOS 为图片添加水印
  • 基于openfeign拦截器RequestInterceptor实现的微服务之间的夹带转发
  • react快速开始项目模板
  • Web基础 -SpringBoot入门 -HTTP-分层解耦 -三层架构
  • Protobuf 深度解析:从基础语法到高级应用
  • 【目标检测】图像处理基础:像素、分辨率与图像格式解析
  • “自动化失败归因”测试集-WhoWhen
  • 在大数据求职面试中如何回答分布式协调与数据挖掘问题
  • 云原生 CAD 让制造业设计协同更便捷
  • 3D模式格式转换工具HOOPS Exchange如何将3D PDF转换为STEP格式?
  • 【实时Linux实战系列】使用定时器实现定时任务
  • 计算机网络:(六)超详细讲解数据链路层 (附带图谱表格更好对比理解)
  • docker镜像中集成act工具
  • 刀客doc:阿里巴巴集团品牌部划归集团公关管理
  • Java基础(三):逻辑运算符详解
  • P3258 [JLOI2014] 松鼠的新家
  • (LeetCode 面试经典 150 题) 27.移除元素
  • PR出书启动
  • ✨通义万相2.1深度解析:AI视频生成引擎FLF2V-14B全流程指南(命令行参数+模型架构+数据流)
  • VTK.js
  • 容声冰箱如何让荔枝在世俱杯赛场外再“长7天”
  • Elasticsearch API访问权限控制:禁用外部端点访问
  • 在Ubuntu上设置Selenium自动化测试环境:Chrome与Firefox的详细指南
  • 海拔案例分享-门店业绩管理小程序
  • 小程序 顶部栏标题栏 下拉滚动 渐显白色背景
  • Python Django全功能框架开发秘籍
  • 多模态大语言模型arxiv论文略读(133)
  • 【nvidia-H100-ib排障实战2】:服务器 InfiniBand 网络性能问题深度分析