Android报错集锦

亲身经历的报错集锦。

在Android开发中会遇到各种奇奇怪怪的报错。很多报错利用搜索引擎能够解决,但是还有一部分是要靠自己摸索才能克服的,于是把这部分报错记录在这篇博客里面,做一个参考。有很多报错原因是有多种的,具体情况需要具体分析,这里只是提供一种解决方案或思路。

文章目录

Error:Could not find com.android.tools.build:gradle:4.1

见:https://blog.csdn.net/zengsidou/article/details/79797417

Error:The SDK directory ‘C:\Users\Darko\AppData\Local\Android\sdk’ does not exist.

见:https://blog.csdn.net/zengsidou/article/details/56845869

Android Studio: cannot resolve symbol R

见:https://blog.csdn.net/zengsidou/article/details/56016931

No resource found that matches the given name “xxx”

见:https://blog.csdn.net/zengsidou/article/details/45933215

No resource found that matches the given name 'Theme.AppCompat.Light"

见:https://blog.csdn.net/zengsidou/article/details/45783925

This element has no attached source and the Javadoc could not be found in the attached Javadoc.

见:https://blog.csdn.net/zengsidou/article/details/45772837

You need to use a Theme.AppCompat theme (or descendant) with this activity

见:https://blog.csdn.net/zengsidou/article/details/45731385

Failed to transform file ‘android.jar’ to match attributes.

报错提示:

Failed to transform file ‘android.jar’ to match attributes.

原因:Android SDK中的android.jar有错误。因为之前替换过android.sdk,导致其不是原版的jar。

解决方案:替换为正确的android.jar,使用SDK Manager或者从官网下载。

CreateProcess error=2, 系统找不到指定的文件。

git设置有误

Cannot resolve external dependency xxx

没有指定当前project的仓库。
本例原因是在根项目的 build.gradle 里面把 allprojects 闭包删除了。加回去即可。

AAPT: error: resource android:attr/dialogCornerRadius not found

sdk版本太高,改低一点

gradle爆红但能正常运行

Clean & invalidate cache

Attempt to invoke virtual method ‘android.content.res.AssetManager android.content.res.Resources.getAssets()’ on a null object reference

覆盖安装后找不到资源,属于系统bug。
见:资源为 null 的问题 https://booster.johnson…

解决方式:卸载apk再重装。

Plugin with id ‘maven‘ not found / Plugin [id: ‘maven‘] was not found in any of the following sources

apply plugin: ‘maven’ 改成: apply plugin: ‘maven-publish’

来源:https://blog.csdn.net/qq_37455727/article/details/119778435

Build was configured to prefer settings repositories over project repositories but repository ‘***’ was added by build file ‘build.gradle’

Gradle 7 之后,仓库设置从 build.gradle - allProjects 移到了 settings.gradle 中。如果在 settings.gradle 里面设置了仓库,则不可以在 build.gradle 中再次设置。

Authentication scheme ‘all’(Authentication) is not supported by protocol ‘file’

通过 gradle 插件发布到 maven 仓库的时候遇到的问题。原因是发布到本地文件不需要认证(?)。

删除脚本中的 credentials 选项即可。

Dependent features configured but no package ID was set.

你添加的依赖包没有设置成library,你可以参考别的依赖把

apply plugin: 'com.android.application’改为
apply plugin: ‘com.android.library’

AndroidStudio No Debuggable Processes

来自:https://blog.csdn.net/ezconn/article/details/90485801

我的导致以上问题的原因是:Android设备里面没有安装或者没有打开debug版的app,所以看不到进程名;

not permitted by network security policy

来自:https://blog.csdn.net/xiexiaotian11/article/details/89006251

原因

高版本 Android 网络配置禁止明文传输。

解决方案

创建个xml文件,添加如下代码:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

在androidMainfest中配置在application中:

    <application
        android:networkSecurityConfig="@xml/network_security_config">
    </application>

this and base files have different roots:

原因:

打包目标文件夹有误。

解决方案:

修改打包目标文件夹。

java.text.ParseException: Unparseable date: “Thu, 14 Apr 2022 07:38:23 GMT”

原因:

在排除掉日期格式有误之后,可能的原因是区域未设置。SimpleDateFormat 会使用默认的区域格式,解析不同区域的格式时可能出错。

解决方案:

对于题目中所示日期格式,将 DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); 改为 DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);

模拟器提示:Hold Shift to control camera

解决方案:

https://www.reddit.com/r/androiddev/comments/stbx60/hold_shift_to_control_camera_in_android_studio/

打开模拟器的相机,按住shift控制一下镜头,然后关闭相机app。

Cannot query the value of this property because it has no value available.

原因:

网上搜索清一色说是SDK版本的问题,但是我的SDK版本没有任何问题。
检查了一番,发现如同提示所说:Cannot query the value of this property because it has no value available. 也就是说,没有定义对应的值。
这是由于我在 gradle 脚本中调用了 android.defaultConfig.versionName 这个值,但是由于该模块类型是 AndroidLibrary,默认没有定义 versionName,所以就报了该错误。

解决方案:
defaultConfig 中加入 versionName 字段。

java.io.FileNotFoundException: No content provider

原因:

路径格式不正确,在设置路径的时候没有被识别为本地文件。

解决方案:

/sdcard/x.mp4 修改为 file:///sdcard/x.mp4

already contains entry ‘assets/xxx’, cannot overwrite

原因:

assets文件夹下不允许重名资源,检查之

解决方案:

删除或重命名重名的文件。如果没有找到的话,可能是导入的aar中有重名的资源。

Unresolved reference: 某Kotlin文件;其实该文件存在,且能够正常跳转

原因:

对应的kotlin文件模块没有引入kotlin plugin。这个问题见于在老的java模块中,新增了kotlin类,但是没有更新对应的build.gradle文件。

解决方案:

在build.gradle 文件中,增加对kotlin插件的引入。

minSdkVersion 17 cannot be smaller than version 19 declared in library

原因:

引用的模块或aar中声明的minSdk大于主模块的minSdk。

解决方案:

要么升级主模块的minSdk,要么在主模块中强制最低sdk版本。具体方式是在Manifest文件中添加:

    <uses-sdk
        android:minSdkVersion="17"
        tools:overrideLibrary="com.example.package,com.example.package2" />

其中填入需要被覆盖的包名;多个不同的包名用逗号分割。具体的包名可以查看该aar对应的Manifest文件。

com.alibaba.fastjson.JSONException: default constructor not found. class

原因:
FastJson转换对象需要默认无参构造方法。

解决方案:
给对象加上无参构造方法,或者添加依赖项implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值