当前位置: 首页 > 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、

相关文章:

  • Linux切换中文输入法
  • 商品中心—11.商品B端搜索系统的实现文档二
  • 腾讯云 CodeBuddy 技术评估报告(2025年):编码效率提升40%,复杂工程处理能力领先Cursor 35%​
  • idea2024里的jar打包(找不到主类解决方法)
  • idea依赖下载慢解决
  • 图形化http api测试工具yunedit-post
  • Web基础 -SpringBoot入门 -HTTP-分层解耦 -三层架构
  • 利用栈,实现括号匹配功能
  • vtkImageData去噪——vtkImageMedian3D
  • 板凳-------Mysql cookbook学习 (十--9)
  • 带约束的高斯牛顿法求解多音信号分离问题
  • GPIO-LED驱动
  • FPGA基础 -- Verilog 验证平台
  • Kimi“新PPT助手” ,Kimi全新自研的免费AI生成PPT助手
  • Android 编译和打包image镜像流程
  • RS485
  • 用于算法性能预测的 GNN 框架
  • 在Ubuntu上设置Firefox自动化测试环境:指定Marionette端口号
  • 【Comosl教程】如何计算物体所受到的力矩——质心积分法
  • 微信小程序:选择页面单选实现(多页面均可选择)
  • 推销什么企业做网站和app/软件发布网
  • 想做cpa 没有网站怎么做/优化的意思
  • 什么软件可以做mv视频网站/百度电脑网页版入口
  • 基于jsp的电商网站开发/网站查询站长工具
  • 打电话拉客户用网站做广告怎么做 好做吗/网络营销方案的制定
  • 什么网站能接单做网站/百度地图人工客服电话