【errors】android.database.CursorIndexOutOfBoundsException:Index -1

本文解决在Android中使用Cursor获取数据库查询结果时遇到的CursorIndexOutOfBoundsException异常。原因是游标初始位置不在有效范围内,解决方法是在提取数据前先调用moveToFirst()或moveToNext()。

第一次使用cursor获取手机本地的歌曲时,读取cursor的内容总会报错

05-13 02:57:08.351: E/AndroidRuntime(6336): Caused by: 
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 4
原因

Android 中数据库处理使用 Cursor 时,游标不是放在为 0 的下标,而时放在为 -1 的下标处开始的,也就是说返回给 Cursor 查询结果时,不能够马上从 Cursor 中提取值。

解决

调用 cursor.moveToFirst() 或moveToNext()。

Executing tasks: [:app:assembleDebug] in project D:\TestProject\MyApplication > Task :app:createDebugVariantModel UP-TO-DATE > Task :app:preBuild UP-TO-DATE > Task :app:preDebugBuild UP-TO-DATE > Task :app:mergeDebugNativeDebugMetadata NO-SOURCE > Task :app:compileDebugAidl NO-SOURCE > Task :app:compileDebugRenderscript NO-SOURCE > Task :app:generateDebugBuildConfig UP-TO-DATE > Task :app:javaPreCompileDebug UP-TO-DATE > Task :app:checkDebugAarMetadata UP-TO-DATE AGPBI: {"kind":"warning","text":"Your project has set `android.useAndroidX=true`, but configuration `:app:debugRuntimeClasspath` still contains legacy support libraries, which may cause runtime issues.\nThis behavior will not be allowed in Android Gradle plugin 8.0.\nPlease use only AndroidX dependencies or set `android.enableJetifier=true` in the `gradle.properties` file to migrate your project to AndroidX (see https://developer.android.com/jetpack/androidx/migrate for more info).\nThe following legacy support libraries are detected:\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> com.android.support:support-annotations:28.0.0\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> com.android.support:collections:28.0.0\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> android.arch.lifecycle:runtime:1.1.1\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> android.arch.lifecycle:runtime:1.1.1 -> android.arch.lifecycle:common:1.1.1\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> android.arch.lifecycle:runtime:1.1.1 -> android.arch.core:common:1.1.1\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> com.android.support:versionedparcelable:28.0.0","sources":[{}]} > Task :app:generateDebugResValues UP-TO-DATE > Task :app:mapDebugSourceSetPaths UP-TO-DATE > Task :app:generateDebugResources UP-TO-DATE > Task :app:mergeDebugResources UP-TO-DATE > Task :app:packageDebugResources UP-TO-DATE > Task :app:parseDebugLocalResources UP-TO-DATE > Task :app:createDebugCompatibleScreenManifests UP-TO-DATE > Task :app:extractDeepLinksDebug UP-TO-DATE > Task :app:processDebugMainManifest FAILED [androidx.versionedparcelable:versionedparcelable:1.1.1] C:\Users\wangsen\.gradle\caches\transforms-3\4b621581b14be943fef7e9025808a5ef\transformed\versionedparcelable-1.1.1\AndroidManifest.xml Warning: Namespace 'androidx.versionedparcelable' used in: androidx.versionedparcelable:versionedparcelable:1.1.1, com.android.support:versionedparcelable:28.0.0. D:\TestProject\MyApplication\app\src\main\AndroidManifest.xml:22:18-91 Error: Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 is also present at [androidx.core:core:1.7.0] AndroidManifest.xml:24:18-86 value=(androidx.core.app.CoreComponentFactory). Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:7:5-25:19 to override. See https://developer.android.com/r/studio-ui/build/manifest-merger for more information about the manifest merger. > Task :app:mergeDebugShaders UP-TO-DATE > Task :app:compileDebugShaders NO-SOURCE > Task :app:generateDebugAssets UP-TO-DATE > Task :app:mergeDebugAssets UP-TO-DATE > Task :app:compressDebugAssets UP-TO-DATE > Task :app:processDebugJavaRes NO-SOURCE > Task :app:desugarDebugFileDependencies UP-TO-DATE > Task :app:mergeDebugJniLibFolders UP-TO-DATE > Task :app:mergeDebugNativeLibs NO-SOURCE > Task :app:stripDebugDebugSymbols NO-SOURCE > Task :app:validateSigningDebug UP-TO-DATE > Task :app:writeDebugAppMetadata UP-TO-DATE > Task :app:writeDebugSigningConfigVersions UP-TO-DATE > Task :app:checkDebugDuplicateClasses FAILED > Task :app:mergeDebugJavaResource FAILED FAILURE: Build completed with 3 failures. 1: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:processDebugMainManifest'. > Manifest merger failed with multiple errors, see logs * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. ============================================================================== 2: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:checkDebugDuplicateClasses'. > A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable > Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.graphics.drawable.IconCompatParcelizer found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.ResultReceiver$1 found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class androidx.core.graphics.drawable.IconCompatParcelizer found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class androidx.core.internal.package-info found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class androidx.versionedparcelable.CustomVersionedParcelable found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.NonParcelField found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.ParcelField found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.ParcelImpl found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.ParcelImpl$1 found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.ParcelUtils found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcel found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcel$1 found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcel$ParcelException found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcelParcel found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcelStream found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcelStream$FieldBuffer found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcelable found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcelize found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Go to the documentation to learn how to <a href="d.android.com/r/tools/classpath-sync-errors">Fix dependency resolution errors</a>. * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. ============================================================================== 3: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:mergeDebugJavaResource'. > A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction > 2 files found with path 'META-INF/androidx.core_core.version' from inputs: - C:\Users\wangsen\.gradle\caches\transforms-3\0b22a6b07b3e498281e2fdb9d93c9c6b\transformed\support-compat-28.0.0\jars\classes.jar - C:\Users\wangsen\.gradle\caches\transforms-3\0212d0c0c0b99c5d00c15a82a811f4d1\transformed\core-1.7.0\jars\classes.jar Adding a packagingOptions block may help, please refer to https://developer.android.com/reference/tools/gradle-api/7.4/com/android/build/api/dsl/ResourcesPackagingOptions for more information * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. ============================================================================== * Get more help at https://help.gradle.org BUILD FAILED in 436ms 22 actionable tasks: 3 executed, 19 up-to-date
07-10
<think>我们面对的问题:Android项目在构建APK时由于混合使用AndroidX和旧版Support库导致的Manifest合并错误和类冲突问题。 具体错误包括: 1. Manifest merger failed: 例如Attribute application@appComponentFactory冲突(引用[5])。 2. Duplicate class: 例如android.support.v4.app.INotificationSideChannel在androidx和support库中重复出现(引用[1][3])。 原因分析:从引用[2][3]中可以看出,这是因为项目同时引用了Android Support库和AndroidX库,而这两个库不能共存。 解决方法有两种(引用[2][4]): 方法一:将整个项目迁移到AndroidX(推荐,因为AndroidX是Support库的替代,且是未来方向)。 方法二:排除冲突的依赖,并确保所有依赖都使用同一种库(要么全部用Support,要么全部用AndroidX)。但考虑到新库可能已经迁移到AndroidX,所以方法一更彻底。 下面详细说明两种方法: ### 方法一:迁移到AndroidX(推荐) 1. **使用Android Studio的迁移工具** - 确保Android Studio版本在3.2以上(建议使用最新版)。 - 在菜单栏选择:Refactor > Migrate to AndroidX... - 按照向导操作,迁移工具会自动替换代码中的Support库类为AndroidX的对应类,并更新gradle文件。 2. **手动配置gradle.properties** 如果迁移工具没有自动添加,可以在项目根目录的`gradle.properties`文件中添加: ```properties android.useAndroidX=true android.enableJetifier=true ``` - `android.useAndroidX=true`:表示使用AndroidX库。 - `android.enableJetifier=true`:自动转换第三方库中的Support库为AndroidX库(解决第三方库冲突)。 3. **修改build.gradle** 确保依赖中使用的是AndroidX库,例如: ```groovy dependencies { implementation 'androidx.appcompat:appcompat:1.0.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' // 其他依赖 } ``` 4. **处理Manifest冲突** 如果迁移后仍然出现Manifest合并错误(如引用[5]中的错误),可以在`AndroidManifest.xml`的`<application>`标签中添加: ```xml <application ... tools:replace="android:appComponentFactory" android:appComponentFactory="androidx.core.app.CoreComponentFactory" > ``` 注意:添加`tools:replace`属性是为了替换冲突的属性,同时指定使用AndroidX的CoreComponentFactory。 ### 方法二:回退到Support库(不推荐,仅临时方案) 如果由于某些原因不能迁移到AndroidX,可以尝试将所有依赖都回退到使用Support库的版本。但注意,新版本的库可能已经不支持Support库了。 1. **检查依赖版本** 在app的build.gradle中,确保所有依赖都是Support库版本,并且版本一致(如28.0.0)。例如: ```groovy dependencies { implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:design:28.0.0' // 避免使用已经迁移到AndroidX的库,例如lottie-android如果使用了AndroidX,则需要降级到支持Support库的版本 } ``` 2. **排除冲突的依赖** 如果某个依赖引入了AndroidX库,可以排除它: ```groovy implementation ('com.example.library:example:1.0') { exclude group: 'androidx.core', module: 'core' } ``` 3. **强制使用Support库版本** 在build.gradle中配置强制使用某个版本(注意:可能导致运行时错误): ```groovy configurations.all { resolutionStrategy { force 'com.android.support:support-v4:28.0.0' force 'com.android.support:appcompat-v7:28.0.0' // 其他冲突的库 } } ``` 4. **处理Manifest冲突** 同样,如果出现Manifest合并错误,可以在`AndroidManifest.xml`的`<application>`标签中添加: ```xml <application ... tools:replace="android:appComponentFactory" android:appComponentFactory="android.support.v4.app.CoreComponentFactory" > ``` ### 注意事项 - 迁移到AndroidX后,代码中所有引用Support库的地方都需要改为AndroidX的对应类(迁移工具会自动完成大部分)。 - 迁移后,需要测试所有功能,因为包名改变可能导致反射等操作失败。 - 如果使用第三方库,确保它们也支持AndroidX,或者使用Jetifier转换(通过`android.enableJetifier=true`)。 根据引用[2][4]的建议,迁移到AndroidX是根本解决方案,因为Google已经停止维护Support库。 希望以上步骤能解决你的问题。 §§相关问题§§ 1. 如何检查项目中哪些依赖还在使用Support库? 2. 迁移到AndroidX后如何解决第三方库导致的运行时错误? 3. Jetifier工具的工作原理是什么?它如何转换第三方库? 4. 如何解决迁移AndroidX后出现的资源文件错误(如布局文件中的类路径错误)? 5. 有没有自动回退到Support库的迁移工具?</think>### 解决 Android Gradle 构建中的 Manifest 合并错误与类冲突问题 当 Android 项目同时使用 AndroidX 和旧版 Support 库时,会导致构建失败,主要出现以下两类错误: 1. **Manifest Merger Failed** (如 `Attribute application@appComponentFactory` 冲突) 2. **Duplicate Class** (如 `android.support.v4.app.INotificationSideChannel` 在多个库中重复) 根本原因是 **AndroidX 和 Support 库无法共存**(引用[2][3]),以下是系统化的解决方案: --- ### 解决方案一:迁移到 AndroidX(推荐) #### 步骤 1:启用 AndroidX 支持 在项目根目录的 `gradle.properties` 文件中添加: ```properties android.useAndroidX=true android.enableJetifier=true ``` - `useAndroidX`:强制使用 AndroidX 库 - `enableJetifier`:自动转换第三方库中的 Support 类为 AndroidX(关键!引用[4]) #### 步骤 2:更新依赖库版本 在 `app/build.gradle` 中替换所有 Support 库依赖: ```gradle dependencies { // 替换前:implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'androidx.appcompat:appcompat:1.6.1' // AndroidX 版本 // 替换前:implementation 'com.android.support:design:28.0.0' implementation 'com.google.android.material:material:1.9.0' // Material Design 库 } ``` #### 步骤 3:修复 Manifest 冲突 在 `AndroidManifest.xml` 的 `<application>` 标签添加: ```xml <application ... tools:replace="android:appComponentFactory" android:appComponentFactory="androidx.core.app.CoreComponentFactory"> </application> ``` `tools:replace` 会覆盖冲突属性(引用[5]) --- ### 解决方案二:强制使用 Support 库(临时方案) #### 步骤 1:排除冲突依赖 ```gradle dependencies { implementation('com.thirdparty.library:1.0') { exclude group: 'androidx.core', module: 'core' // 排除 AndroidX } implementation 'com.android.support:support-compat:28.0.0' // 明确使用 Support } ``` #### 步骤 2:统一所有库版本 在 `build.gradle` 中强制统一版本: ```gradle configurations.all { resolutionStrategy { force 'com.android.support:support-v4:28.0.0' force 'com.android.support:appcompat-v7:28.0.0' } } ``` --- ### 关键注意事项 1. **Jetifier 的作用** 当 `enableJetifier=true` 时,Gradle 会自动重写第三方库的字节码,将其中的 `android.support.*` 替换为 `androidx.*`(引用[4]) 2. **迁移后的代码调整** - 将 Java 代码中的 `import android.support.v7.app.AppCompatActivity` 改为 `import androidx.appcompat.app.AppCompatActivity` - 布局文件中的 `android.support.design.widget.TextInputLayout` 改为 `com.google.android.material.textfield.TextInputLayout` 3. **清理构建缓存** 执行以下命令清除缓存: ```bash ./gradlew cleanBuildCache ``` --- ### 验证是否解决 1. 检查构建日志中是否还有 `Duplicate class` 或 `Manifest merger` 错误 2. 使用以下命令确认依赖关系: ```bash ./gradlew :app:dependencies ``` 输出中不应同时出现 `androidx.*` 和 `com.android.support.*` > **推荐策略**:优先采用方案一(迁移到 AndroidX),因为 Google 已停止维护 Support 库,且新版本库(如 Jetpack Compose)仅支持 AndroidX(引用[2][4])。 --- ### 相关问题 1. 如何检测项目中哪些第三方库仍在使用 Support 库? 2. 迁移到 AndroidX 后出现资源 ID 冲突(`R.id` 重复)该如何解决? 3. AndroidX 迁移工具在哪些情况下会失败?如何手动修复迁移后的代码? 4. 如何解决多模块项目中部分模块未迁移导致的构建冲突? 5. Jetifier 转换库的原理是什么?它如何影响构建性能?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值