Android与kotlin报错

前言

记录下我遇到kotlin的错误与解决方法,希望对你有所帮助
记录我遇到的kotlin与android的问题
第二篇Android与kotlin报错2
第三篇Android与kotlin报错3

错误信息

Kotlin: Cannot access java.io.Serializable’ which is a supertype of ‘kotlin.Int’. Check your module classpath for missing or conflicting dependencies

解决方法

I got the same error today after Intellij upgraded my bundled kotlin plugin on Mac OS. The problem might not be the same, but the error message was identical.

Turns out before my project was using JDK 11, but after the kotlin plugin upgrade the JDK was upgraded as well to 13. I resolved the issue by dropping the existing JDK 11 with the - button in the Project Structure window and then adding it again using the + button - which selected JDK 13 by default. After that this error went away.
将项目结构中sdk旧版全删除,只保留14
如果提示项目没有sdk,再按照提示下载即可

引用

https://discuss.kotlinlang.org/t/cannot-access-java-io-serializable-which-is-a-supertype-of/16595/3

错误信息

Error:(9, 1) Kotlin: Conflicting overloads: public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file three.kt, public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file two.kt

Error:(6, 15) Kotlin: Overload resolution ambiguity:
public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file three.kt
public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file two.kt

Error:(3, 1) Kotlin: Conflicting overloads: public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file three.kt, public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file two.kt
D:\IDEA\20200524-1\src\two.kt
D:\IDEA\20200524-1\src\three.kt

原因

不同文件中不能定义相同名称的函数,否则报错

解决

注释掉相同名称的函数

错误信息

Overload resolution ambiguity. All these functions match

原因

变量的引用模糊不清

解决

重新定义变量名

错误信息

Unresolved reference: name5

解决

只要设置权限修饰符在要调用的类就能解决

错误信息

An operation is not implemented: Not yet implemented

原因

没有重写方法

解决

将重写的方法生成的todo删除即可,如下
TODO("Not yet implemented")

总结

todo标识符如果没解决,会有运行时异常

错误信息

Too many arguments for public constructor Cellphone( defined in数据类与单例类. Cellphone

原因

构造器Cellphone名称 太多而引发的冲突

解决

将多个Cellphone改名

错误信息

Error running ‘app’: Default Activity not found

原因

找不到主程序来进行启动

解决

注意activity标签的前缀和后缀是否在合适的位置

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".FirstActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
    </application>

错误信息

已经有布局,但是无法识别,提示Unresolved reference: right. fragment

解决

复制布局的代码,删除已有布局,用万能键创建布局,粘贴进去,就能识别

错误信息

Unresolved reference: from
在这里插入图片描述

解决

注意区别标红语句的那一段每个字母的大小写,如果看不出来可以复制别人的相同代码然后粘贴就能识别

错误信息

Failed to install the following Android SDK packages as some licences have no been accepted

解决

在下方build,在最下方提示install xxxxxx ,点击安装即可
在这里插入图片描述

十一

错误信息

Caused by: java. lang. ClassCastException: android. widget . FrameLayout cannot be cast to com. example . a20200616study . NewsContentFragment
at com.example . a20200616study . NewsContentActivity . onCreate (NewsContentActivity.kt:28)

解决

有多个相同名称的变量,要注意导包,别导错了
若不会用debug,要善于用ait+变量名与ctrl+f来进行查代码

十二

错误信息

java报错 Cannot resolve symbol ‘java’ Unused import statement

解决

java报错 Cannot resolve symbol ‘java’ Unused import statement

十三

错误信息

ERROR: Gradle version 2.2 is required. Current version is 6.2-rc-3.

解决

报错:ERROR: Gradle version 2.2 is required. Current version is 6.2-rc-3.

十四

错误信息

‘androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner’ for AndroidJUnit4 could not be loade

解决

将import androidx.test.ext.junit.runners.AndroidJUnit4;替换成import androidx.test.runner.AndroidJUnit4;

十五

错误信息

找不到或无法加载主类

解决

在这里插入图片描述

引用

IDEA 错误: 找不到或无法加载主类 解决方法

十六

错误信息

android.view.InflateException: Binary XML file line #2 in com.example.materialcardview:layout/fruit_item: Binary XML file line #2 in com.example.materialcardview:layout/fruit_item: Error inflating class com.google.android.material.card.MaterialCardView
布局无法解析当前组件,同时调用不了对象的方法

解决

如果布局无法解析,对象调用不了相应的方法,而没有拼写错误,就是没有添加依赖

添加相应的依赖即可

十七

错误信息

Invoke-customs are only supported starting with Android O (–min-api 26)

解决

build.gradle(:app)中的minSdkVersion设置为26即可

十八

错误信息

java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

解决

java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme t

十九

错误信息

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)’ on a null object reference

解决办法

添加setSupportActionBar(toolbar);在getSupportActionBar().setDisplayHomeAsUpEnabled(true);之前

二十

错误信息

ERROR: Failed to resolve: org.junit.jupiter:junit-jupiter:
Affected Modules: app

解决办法

在build.gradle(:app)中的dependencies闭包中删除关于jupiter的引用
例: implementation ‘org.junit.jupiter:junit-jupiter’

二十一

错误信息

Cannot resolve symbol ‘DBOpenHelper1’

解决办法

如果确定有这个变量或者类,那就在类或者变量名复制粘贴,让AS重新识别,或者删掉,手动重新输,看AS会不会给提示

二十二

错误信息

错误: 找不到符号
setContentView(R.layout.activity.login);
^
符号: 变量 activity
在这里插入图片描述

解决办法

重启AndroidStudio
引用 AndroidStudio 3.6 中 R.layout 找不到对应的xml文件

二十三

错误信息

布局加载空白
在这里插入图片描述

解决办法

重载另一个常用的OnCreate(@Nullable Bundle savedInstanceState)方法

引用
关于onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState)的问题

二十四

错误信息

java报错 Cannot resolve symbol ‘java’ Unused import statement

可能原因

idea 卸载重新下载时候,配置发生错误,无法识别sdk

解决办法

在这里插入图片描述

在这里插入图片描述
在下拉选项中换一个即可

二十五

错误信息

错误:无效的源发行版:11

解决方法:

在这里插入图片描述
将 project language level 中选择sdk default 即可,如图所示

参考网址

https://blog.csdn.net/qq496013218/article/details/56845485

二十六

错误信息

报错:ERROR: Gradle version 2.2 is required. Current version is 6.2-rc-3.

解决办法

导入别人的项目出现这个问题:

改build gradle(第一个),依赖的 classpath,新建项目然后复制替换进去即可

然后出现ERROR: Could not find com.android.tools.build:gradle:3.5.3.

在buildscript和allprojects的repositories下添加google(),然后同步即可

然后出现ERROR: Your project path contains non-ASCII characters. This will most likely cause the build to fail on Windows. Please move your project to a different directory. See http://b.android.com/95744 for details. This warning can be disabled by adding the line ‘android.overridePathCheck=true’ to gradle.properties file in the project directory.

看到编码和目录就想到as不支持中文,把项目复制粘贴到一个新的英文文件夹,然后导入即可

ERROR: Failed to resolve: com.android.support:appcompat-v7:26.2.1

在app module的build gradle 中 在依赖中,将com.android.support:appcompat-v7:26.2.1改成 com.android.support:appcompat-v7:26+’

二十七

错误信息

net start mysql 发生系统错误2 系统找不到指定的文件

解决办法

原文出处:https://www.jianshu.com/p/6d8ed7c36e6f
原文出处:https://www.cnblogs.com/caiyishuai/p/10783198.html

二十八

错误信息

Could not resolve all files for configuration ‘:app:debugCompileClasspath’

解决办法

原文如下:https://blog.csdn.net/wjj1996825/article/details/80900875
https://www.jianshu.com/p/9de30197b528

二十九

错误信息

JUnit Test 测试 initializationError错误

解决办法

Spring框架与JUnit版本不兼容,我的Spring4.3.18,JUnit4.12,将JUnit降至4.4左右版本即可。
引用:https://www.cnblogs.com/umeall/p/9236233.html

三十

错误信息

‘androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner’ for AndroidJUnit4 could not be loade

解决办法

将import androidx.test.ext.junit.runners.AndroidJUnit4;替换成import androidx.test.runner.AndroidJUnit4;

三十一

错误信息

关于使用dataBinding时无法自动生成binding类的问题

解决办法

重启as。。
在这里插入图片描述

三十二

错误信息

在xml中找不到viewmodel的get方法
在这里插入图片描述

解决办法

viewmodel中get方法中的get后面第一个字母要大写
在这里插入图片描述
在这里插入图片描述

三十三

错误信息

Failed to install the following Android SDK packages as some licences have not been accepted.
build-tools;30.0.2 Android SDK Build-Tools 30.0.2
To build this project, accept the SDK license agreements and install the missing components using the Android Studio SDK Manager.
Alternatively, to transfer the license agreements from one workstation to another, see http://d.android.com/r/studio-ui/export-licenses.html

Using Android SDK: C:\Users\Administrator\AppData\Local\Android\Sdk

在这里插入图片描述

解决办法

License for package Android SDK Build-Tools 29.0.2not accepted

三十四

错误信息

在这里插入图片描述
����: ���� lose_score_message
�: �� string
报错乱码

解决方法

Android Studio 4.1 Build状态栏输出出现中文乱码的问题
设置完,关闭as,再重启

三十五

错误信息

在这里插入图片描述

解决办法

注意String.xml中关于变量的name的拼写

三十六

错误信息

错误: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public class Word {
^
Tried the following constructors but they failed to match:

解决办法

可以构建一个空的构造函数 public Word(){}

三十七

错误信息

Error:Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy settings either in IDE or Gradle

解决办法

Error:Connection timed out: connect

三十八

错误信息

Cause: android-apt plugin is incompatible with the Android Gradle plugin. Please use ‘annotationProcessor’ configuration instead.

解决办法

Error:android-apt plugin is incompatible with the Android Gradle plugin. Please use 'annotationProce

三十九

错误信息

Cannot resolve constructor ‘ViewModelProvider
(com.example.myapplication.WinFragment,androidx.lifecycle.ViewModelProvider.NewInstanceFactory)’

解决办法

如果出现找不到构造器的提示,就删除就删除import导包,重新导一遍
在这里插入图片描述

四十

错误信息

错误: 找不到符号
import com.example.viewmodelsavedstate.databinding.ActivityMainBindingImpl;
^
符号: 类 ActivityMainBindingImpl
位置: 程序包 com.example.viewmodelsavedstate.databinding

解决办法

在XML界面文件上出错了,重点检查databinding的界面语法,比如<variable>, @{ }中的内容。

参考

错误: 找不到符号 符号: 类 ActivityMainBindingImpl 位置: 程序包 com.example.databinding.databinding

四十一

错误信息

Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all artifacts for configuration ‘:classpath’.

解决办法

Android Studio 报 Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfig 的错误

四十二

错误信息

A problem occurred configuring root project ‘test2’.
Could not resolve all artifacts for configuration ‘:classpath’.
Could not find com.android.tools.build:gradle:6.9.1.、

解决办法

在这里插入图片描述

更改build.gradle(app) buildscript下的dependencies下的classpath的gradle的版本号

四十三

错误信息

在idea运行不了 android程序可以尝试
换build.gradle(:app)的 classpath "com.android.tools.build:gradle:3.5.0"里面的gradle:xxx

在https://services.gradle.org/distributions/中下载gradle,然后存放在 C:\Users\JF628.gradle\wrapper\dists 目录下,在gradle-wrapper.properties中更换distributionUrl=https://services.gradle.org/distributions/gradle-7.2-all.zip

四十四

错误信息

AVD XXX is already running. If that is not the case, delete the files at xxx*.lock and try again

解决办法

AVD XXX is already running. If that is not the case, delete the files at xxx*.lock and try again解决方法

四十五

错误信息

Unable to create Debug Bridge: Unable to start adb server: error: protocol fault (couldn’t read status): Connection reset by peer
‘D:\it\Android\sdk\platform-tools\adb.exe start-server’ failed – run manually if necessary

解决办法

Unable to create Debug Bridge:Unable to start adb server
结束5037号端口号的占用,然后重启AS

四十六

错误信息

Installed Build Tools revision 31.0.0 is corrupted. Remove and install again using the SDK Manager.

解决办法

在这里插入图片描述
取消安装API Level 31 安装API Level 30

在这里插入图片描述
取消安装31.0.0,安装30.0.3
在这里插入图片描述
在这里插入图片描述
出处
2021-07-19 Android studio:Installed build tools revision 31.0.0 is corrupted.Remove and install ag

四十七

错误信息

Manifest merger failed : Apps targeting Android 12 and higher are required to specify an explicit value for android:exported when the corresponding component has an intent filter defined.

解决办法

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
出处
Manifest merger failed : Apps targeting Android 12 and higher are required to specify an explicit

四十八

错误信息

Execution failed for task ‘:app:packageRelease’.

A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
com.android.ide.common.signing.KeytoolException: Failed to read key key0 from store “D:\it\Android\key\key.jks”: Get Key failed: getSecretKey failed: Password is not ASCII

解决办法

百度翻译了一下,发现key密码设置的不合法。重新新建一个key,密码设置字母和数字混搭,解决。

四十九

错误信息

java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class kotlinx.coroutines.AbstractCoroutine found in modules jetified-kotlinx-coroutines-core-1.3.0.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0) and jetified-kotlinx-coroutines-core-jvm-1.4.3.jar (org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.4.3)

解决办法

将implementation "androidx.paging:paging-runtime-ktx:3.0.1"换成implementation ‘androidx.paging:paging-runtime-ktx:2.1.1’

实验过程

在everything中搜索冲突jar包的关键词(coroutines),然后把冲突的jar包删除,再去build.gradle(:app)中的dependencies闭包中,一个一个删除所对应的依赖(删除前记得备份),然后删除再rebuild Project,看看冲突的jar包在哪一个依赖中生成,就将该依赖降低版本(可在https://developer.android.google.cn/中查询)

结论

依赖能用就行,别升最高,最高有可能都是bug

五十

错误信息

你的主机中的软件中止了一个已建立的连接。

解决办法

1.如果开发中不需要电脑的 Wi-Fi 热点,关闭热点就可以了;
2.如果需要热点,那么将 Gradle 回退到 6.5 之前的版本,注意,Android 的 Gradle 插件 版本也要同步回退。
来源:
Android Studio:你的主机中的软件中止了一个已建立的连接。

五十一

错误信息

The following classes could not be found:
- androidx.fragment.app.FragmentContainerView

解决办法

在应用或模块的 build.gradle 文件中添加所需工件的依赖项:
implementation “androidx.fragment:fragment:1.3.6”
来源
FragmentContainerView not found

五十二

错误信息

在kotlin无法用id来引用xml的组件

解决办法

使用View Binding
在这里插入图片描述

viewBinding{
    enabled=true
}

在这里插入图片描述

package com.example.gitdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.gitdemo.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding:ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
       binding= ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.textView.text="helloworld"
    }
}

五十三

错误信息

Timed out after 300seconds waiting for emulator to come online.

解决办法

Although there could be various reasons for this behaviour… Mostly it may be due to error in system images or the API version you are using

better you download latest system images and make sure the compatibilty of API version you are using.

enter image description here

If problem exists

I would like to suggest some fixes: Try them

1)Uninstall/Reinstall Android SDK to folder.

OR

  1. delete the emulator and create another emulator targeted at the appropriate API Level.

OR

3)create a new Virtual Device.

OR

4)set “Max VM application heap size” hardware property to 192 or higher and pick the QVGA skin.

hope it helps.

卸载adb,重装其他型号的adb

来源

Error while waiting for device: Timed out after 300seconds waiting for emulator to come online

五十四

错误信息

Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id ‘com.android.internal.application’]

解决办法

将项目的路径改为纯英文

来源

Android Studio出现Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to app
评论区

五十五

错误信息

Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6. Please specify proper ‘jvm-target’ option

解决办法

在build.gradle(:app) , android闭包下,输入
kotlinOptions{ jvmTarget=1.8 }

五十六

错误信息

Unresolved reference: findNavController

解决办法

依赖添加错误,添加以下依赖
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2' implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'

总结

一般来说,方法找不到,就是依赖引用错误

五十七

错误信息

Could not initialize class org.codehaus.groovy.classgen.Verifier

解决办法

在Settings->Build,Execution,Deployment->Build Tools->Gradle 选择Gradle user home的路径

五十八

错误信息

Could not initialize class org.codehaus.groovy.vmplugin.v7.Java7

错误原因

错误原因:gradle版本太低

解决办法

修改gradle-wrapper.properties文件 , 使用6.3版本的Gradle
distributionUrl=https://services.gradle.org/distributions/gradle-6.3-all.zip

引用

类org.codehaus.groovy.vmplugin.v7.Java7找不到

五十九

错误信息

Minimum supported Gradle version is 6.7.1. Current version is 5.6.4.

解决办法

在https://services.gradle.org/distributions/中下载6.7.1以上的gradle文件,放在 C:\Users\JF628.gradle\wrapper\dists 目录下,然后到gradle-wrapper.properties中更改distributionUrl路径,与下载的gradle名称一样

引用

关于Gradle版本问题

六十

错误信息

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.drawerdemo/com.example.drawerdemo.MainActivity}: java.lang.IllegalStateException: Activity com.example.drawerdemo.MainActivity@f167fcb does not have a NavController set on 2131230883

解决办法

private fun initNavigation() {
     //获取Navigation的导航控制器 NavController
     val navHostFragment =
         supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
     val navController = navHostFragment.navController
     //把BottomNavigationView与NavController绑定。
     binding.bottomNavView.setupWithNavController(navController)
 }

出处

Navigation Activity xxxxxx does not have a NavController set on 2131230958

六十一

错误信息

在这里插入图片描述

Avd是灰色的,显示No Devices

解决办法

点击Add Configuration , 点击加号add new Configurations 选择Android app,选择Moudule 和Before launch

六十二

错误信息

viewbinding引用不了xml中include的引用的另一个xml文件的组件

解决办法

先在include中设置一个id

 <include
        layout="@layout/content_layout"
        android:id="@+id/include_head"
        />

然后在activity中引用
binding.includeHead.toolbar

引用

Android ViewBinding更新,include得到优化

六十三

错误信息

Gradle project sync failed. Please fix your project and try again.

解决办法

编辑gradle-wrapper.properties文件,把路径写成正确的,有一个反斜杠要去掉。

来源

Gradle project sync failed的解决方法

六十三

错误信息

在a布局中include进去b布局,显示一片空白,显示不了b布局的组件

错误原因

b本身的布局的组件高度和宽度显示的位置超出include的(a布局)显示范围,所以显示不了
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
删除marginTop属性调整一下距离顶端的位置
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
或者调整a布局的高度和宽度。使其能够容纳下b布局
在这里插入图片描述

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值