前言
记录我遇到的kotlin与android的问题
第一篇Android与kotlin报错
第三篇Android与kotlin报错3
六十四
错误信息
图标和文字对不齐
解决办法
连线,把文字的上端与图标的下端连接起来
六十五
错误信息
Lint found fatal errors while assembling a release target.
解决办法
修改build.gradle(:app) 里的targetSdkVersion版本号
六十六
错误信息
lateinit property context has not been initialized
解决办法
application没有注册就可能导致应用的方法无法调用
注册application
六十七
错误信息
TabLayout无法调用setupWithViewPager方法
解决办法
是TabLayout 不是TableLayout 这是两个控件,你用错了
将tablelayout改成tablayout
如果还不行,就导入以下这个包
implementation(“com.android.support:design:28.0.0”)
来源
我的setupWithViewPager这个方法为什么会报红?谢谢
错误信息
Type mismatch.
Required:
android.support.v4.view.ViewPager?
Found:
androidx.viewpager.widget.ViewPager
解决办法
将整个项目升级到AndroidX。
升级Android Studio后,在 Refactor —> Migrate to AndroidX 路径下可以将整个项目升级到AndroidX。
来源
android.support升级到androidx踩坑记录
六十八
错误信息
A problem occurred evaluating settings ‘mydiary-main’.
Could not find method dependencyResolutionManagement() for arguments [settings_5ga1sxw4u2n9vtftquyztpke1$_run_closure1@c3b9a33] on settings ‘mydiary-main’ of type org.gradle.initialization.DefaultSettings.
解决办法
在setting.gradle中
只留下
rootProject.name = “MyDiary”
include ‘:app’
六十九
错误信息
在as中kotlin不能直接用id来获取xml组件
解决办法
在build:gradle(Module)中 开头换成
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
android {
七十
错误信息
Unparseable date: “”
报错原因
date获取的值为空
解决办法
调整获取值变量的位置,使其能够识别获取
参考来源
经验
善于用log和Toast去进行测试
关于获取EditText编辑框中的值为空的问题
七十一
错误信息
cannot find implementation for com.example.test1.db.AppDataBase. AppDataBase_Impl does not exist
解决办法
在依赖中添加
implementation "androidx.room:room-ktx:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"
参考来源
解决:cannot find implementation for XXX. XXX_Impl does not exist
七十二
错误信息
用kotlin伴生对象生成全局context,运行时报错,lateinit property context has not been initialized
解决办法
可能是manifest中没有注册该kt文件,继承Application的类也需要注册
七十三
错误信息
You need to use a Theme.AppCompat theme (or descendant) with this activity.
解决办法
将AndroidManifest.xml文件中关于Activity的主题配置改成
android:theme="@style/Theme.AppCompat.Light.NoActionBar
参考来源
You need to use a Theme.AppCompat theme (or descendant) with this activity. 完美解决
七十三
错误信息
Room cannot verify the data integrity. Looks like you’ve changed schema but forgot to update the version number. You can simply fix this by increasing the version number.
解决办法
将应用卸载,再运行就能解决
七十四
错误信息
menu菜单无法显示
解决办法
修改themes中的界面主题
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
参考来源
Android Menu菜单-菜单无法显示_Primer5-程序员秘密
七十五
错误信息
recyclerview删除item的时候,报错, IndexOutOfBoundsException: Index: 1, Size: 1
错误原因
数组越界导致
在RecyclerViewAdapter的类里重写了getItemCount()方法,更新数据集合时,长度有变化,试图也变化了,但是getItemCount()方法并没有并没有更新,导致getItemCount()返回的数据集合的长度值与当前的数据集合的长度值不一致,出现以上的错误。
解决办法
解决这类问题的方法就是及时刷新RecyclerViewAdapter的数据
保证当前的数据集合与adapter的内部数据集合一致
以下有几个方法:
notifyItemRangeRemoved()
notifyItemRangeInserted()
notifyItemRangeChanged()
notifyDataSetChanged()
例子
view.show_note_dialog_del.setOnClickListener {
//todo 15:40 对照java版本,对照kotlin的删除数据库的方法
GlobalScope.launch(Dispatchers.Main) {
noteRepository.del(data.id)
notifyDataSetChanged()
mDataList.removeAt(position)
notifyItemRemoved(position)
}
dialog.dismiss()
}
参考来源
RecyclerView 出现java.lang.IndexOutOfBoundsException: Index: 13, Size: 12 问题解决
七十六
错误信息
An exception occurred applying plugin request [id: ‘com.android.application’]
Failed to apply plugin ‘com.android.internal.application’.
Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
You can try some of the following options:
- changing the IDE settings.
- changing the JAVA_HOME environment variable.
- changingorg.gradle.java.home
ingradle.properties
.
解决办法
If the JVM points to version 1.8 then you should change it in settings. You can find it in Preferences → Build, Execution, Deployment → Build Tools → Gradle → *Gradle JDK.
参考来源
Error message “Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8”
七十七
报错信息
编写简单的播放网络资源的mediaplayer,代码没错,界面能进去,但是无法播放,在模拟器能播放,在真机无法播放
解决办法
APP改用https请求
或多等一会
原因
可能是无法http请求
或者是正在下载,等下载完毕后就能播放
参考来源
(Android MediaPlayer无法在API 26+上播放音乐)[https://www.it1352.com/1908983.html]
(Android高版本联网失败报错:Cleartext HTTP traffic to xxx not permitted解决方法)[https://blog.csdn.net/gengkui9897/article/details/82863966]
报错信息
java.io.IOException: Prepare failed.: status=0x1
解决办法
mediaPlayer.setDataSource("http://www.ytmp3.cn/down/57799.mp3")
mediaPlayer.prepareAsync()
mediaPlayer.setOnPreparedListener {
mediaPlayer.start()
}
或者用协程
GlobalScope.launch(Dispatchers.IO) {
}
七十八
报错信息
GsonConverterFactory.create()找不到类
解决办法
依赖添加
compile ‘com.squareup.retrofit2:converter-gson:2.0.2’
参考来源
GsonConverterFactory.create()找不到类
七十九
报错信息
Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ‘:app:debugRuntimeClasspath’.
解决办法
在as中选择file->setting->gradle->选择正确的gradle user home
例 C:\Users\moli.gradle\wrapper\dists
参考来源
gradle出现编译问题Error:Could not resolve all dependencies for configuration ‘:app:debugRuntimeClasspath’.
八十
报错信息
Unresolved reference: liveData
解决办法
添加依赖
implementation "android.arch.lifecycle:extensions:1.1.1"
参考来源
八十一
报错信息
Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.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-26.1.0-runtime (com.android.support:support-compat:26.1.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-26.1.0-runtime (com.android.support:support-compat:26.1.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-26.1.0-runtime (com.android.support:support-compat:26.1.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-26.1.0-runtime (com.android.support:support-compat:26.1.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-26.1.0-runtime (com.android.support:support-compat:26.1.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-26.1.0-runtime (com.android.support:support-compat:26.1.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-26.1.0-runtime (com.android.support:support-compat:26.1.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-26.1.0-runtime (com.android.support:support-compat:26.1.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-26.1.0-runtime (com.android.support:support-compat:26.1.0)
Go to the documentation to learn how to Fix dependency resolution errors.
解决办法
需要在gradle.properties中添加下面两行代码
这是因为混合支持库。通过添加这些行选择androidX作为您的支持库
android.useAndroidX=true
android.enableJetifier=true
参考来源
Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes?
八十二
报错信息
AndroidStudio无法自动连接模拟器,每次连接都用命令提示符连接
解决办法
复制adb.exe所在的路径(AndroidSDK路径下的platform-tools目录内)
例:D:\Software\AndroidStudio\AndroidSDK\platform-tools
粘贴到环境变量里的path中
参考来源
八十三
报错信息
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test1/com.example.test1.MainActivity}: android.view.InflateException: Binary XML file line #19: Binary XML file line #19: Error inflating class ImageView
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070091
解决办法
将图片删了,然后复制粘贴到drawable,然后再重新装填图片
八十四
报错信息
Inheritance from an interface with ‘@JvmDefault‘ members is only allowed with -Xjvm-default option
解决办法
直接在kotlinOptions中添加如下内容
freeCompilerArgs += [
“-Xjvm-default=all”,
]
参考来源
Inheritance from an interface with ‘@JvmDefault‘ members is only allowed with -Xjvm-default option
八十五
报错信息
Caused by: java.lang.RuntimeException: cannot find implementation for com.example.datastorage.WordRoomDatabase. WordRoomDatabase_Impl does not exist
解决办法
//主要就是这三个,你要看是否一致,使用是否正确,如果你使用了kotlin 三个缺一不可
implementation "android.arch.persistence.room:runtime:1.1.1"
//注意看 后缀还不一样 room:compiler
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
参考来源
八十六
报错信息
安装时提示app签名不一致,应用可能被修改
解决办法
修改build.gradle文件中的applicationId
参考来源
八十七
报错信息
Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.7.0-runtime
解决办法
在gradle.properties中添加下面两行代码
android.useAndroidX=true
android.enableJetifier=true
参考来源
Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes?
八十八
报错信息
导入依赖后,在activity或者fragment中无法使用依赖所包含的类
解决办法
在setting.gradle中 添加jcenter()的库
八十九
报错信息
打开相册时,选定相册的图片后会闪退(相当于finish())
解决办法
在manifest的application标签下面添加一行
android:requestLegacyExternalStorage="true"
参考来源
当针对Android 10时,WRITE_外部存储不再提供写入访问
九十
报错信息
EditText获取的值为空
错误原因
没刷新ui
解决办法
在ui刷新页面的方法里操作,例如在按钮的点击事件中进行操作
九十一
报错信息
使用BottomNavigationView做底部导航,menu、MainActivity、activity_main、navigation 都设置完了,运行时只显示界面,点击下方的BottomNavigationView不能进行fragment的切换
报错原因
navigation 与menu与appBarConfiguration内id不一致
解决办法
navigation 与menu与appBarConfiguration内id必须一一对应并且一致
九十二
报错信息
启动androidstudio提示 Failed to load JVM DLL if you already have a 64-bit JDK installed,define a JAVA_HOME variable in xxx
解决办法
下载jdk 配置jdk环境变量
JAVA环境变量配置详解(全网最新详细教程)
九十三
报错信息
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.secondhandtest/com.example.secondhandtest.MainActivity}: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #21: Binary XML file line #21: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
解决办法
在fragment中使用findviewbyid() 而不是使用viewbinding
在fragment中使用findviewbyid()
参考代码
https://github.com/1120026847/BottomNavigationView
九十四
报错信息
获取不了bmob数据,bmob获取到的list集合为Goods@99ceb1,在recyclerview显示不出内容
报错原因
实体类出现错误
出现这种原因,最好把要实现的地方分离出来,然后逐个排查
解决办法
检查实体类继承的数据类型是否正确
九十五
报错信息
bmob无法获取user字段的值,只能获取实体类的默认值
解决办法
如果你已经有使用以下代码关联实体类和用户信息
val user:User=BmobUser.getCurrentUser(User::class.java)
你要确保bmob云数据库中的字段名和实体类的字段名一致
九十六
报错信息
recyclerview显示位置有所偏差,显示偏下面
报错原因
在activity_main.xml中用fragment引用navigation,navigation的startDestination是recyclerview的界面
androidx.constraintlayout.widget.ConstraintLayout的布局中fragment会自动偏下
这是布局,使用fragment标签引用navigation
navigation首个启动导航是navigation_explore,那个fragment就是显示recyclerview
解决办法
在主布局activity_main.xml中 使用线性布局LinearLayout,并调整fragment的位置
九十七
报错信息
Android使用支付宝沙盒报错,提示String的长度是空指针异常
Attempt to invoke virtual method ‘int java.lang.String.length()’
解决办法
检查demo中的RSA2_PRIVATE与RSA_PRIVATE输入是否正确
RSA2_PRIVATE的值为
RSA_PRIVATE的值为
或者将RSA2_PRIVATE与RSA_PRIVATE数值对调
2.
在SignUtils中的sign的KeyFactory的参数中,删除第二个参数
九十八
报错信息
支付宝沙盒点击支付时提示
I/msp: {resultStatus=4000, result=, memo=系统繁忙,请稍后再试}
解决办法
1.没有安装支付宝沙盒app
2.代码要在主程序的oncreate下添加一行代码表示沙盒测试,才能启动支付宝沙盒app
EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
2.检查公钥和私钥
3.没配置activity
参考来源
【支付宝】支付宝支付失败:4000
支付宝 支付返回 4000 ,系统繁忙请稍后再试
支付宝支付返回错误码4000
九十九
报错信息
支付宝沙盒支付提示 客户端报错“商户合作协议已到期,无法继续使用,请联系商户处理。(ALIN42274)
解决办法
联系支付宝客服,提供自己的appid,让他们去重新挂载功能包
一百
报错信息
Android Studio3.5+中文字体不一致的问题
解决办法
Android Studio升级3.5之后,会出现中文字体不一致的问题,原因是升级后,部分英文字体不包含中文字库,解决方法:
如果没有显示Current editor font:xxx–defined in the color scheme,则直接修改Editor->Font->Fallback font中的字体即可,比如SimHei。
如果显示了Current editor font:xxx–defined in the color scheme,则应该修改Editor->Color Scheme->Color Scheme Font->Fallback font中的字体,比如SimHei。