那些年,我们在开发中遇到的错误

记录自己在编程开发遇到的错误,及其解决方法

Java

多线程问题

Exception in thread “Thread-0” java.lang.IllegalMonitorStateException

解决方法:在线程中调用wait方法的时候 要用synchronized锁住对象,确保代码段不会被多个线程调用。

public class Alternate {
	//交替输出1a 2b ...
    public void a1Alternate(){
        Thread thread1 = new Thread(() -> {
            try {
                synchronized (this){
                    for (int i = 1; i < 11; i++) {
                        System.out.println(i);
                        this.notify();
                        this.wait();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                synchronized (this){
                    for (int i = 0; i < 10; i++) {
                        System.out.println((char) ('a'+ i) );
                        this.notify();
                        this.wait();
                    }
                }
            } catch (InterruptedException e) {
                    e.printStackTrace();
            }
        });

        thread1.start();
        thread2.start();
    }

    public static void main(String[] args) {
        Alternate a =new Alternate();
        a.a1Alternate();
    }

}

具体分析可见:Exception in thread “Thread-0” java.lang.IllegalMonitorStateException

Room相关

配置问题

Schema export directory is not provided to the annotation processor so we cannot export the schema.

Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide room.schemaLocation annotation processor argument OR set exportSchema to false.
未向批注处理器提供架构导出目录,因此无法导出架构。你可以提供room.schemaLocation注释处理器参数或将exportSchema设置为false。

直接原因:添加依赖时只添加了Room相关的依赖,遗漏了annotationProcessorOptions。
根本原因:在编译时,Room 会将数据库的架构信息导出为 JSON 文件(默认exportSchema = true导出架构)。要导出架构,需要在 build.gradle 文件中设置 room.schemaLocation 注释处理器属性(设置将json存放的位置)。

我们没有设置exportSchema = false不导出架构或者没有设置架构导出的位置,所以构建错误
解决方法
1、在build gradle中添加(推荐)

    android {
        ...
        defaultConfig {
            ...
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = ["room.schemaLocation":
                                 "$projectDir/schemas".toString()]
                }
            }
        }
    }

2、在数据库注解中添加exportSchema = false(不推荐)

@Database(entities = {entity.class}, version = 4, exportSchema = false)

参考文章:Schema export directory is not provided to the annotation processor so we cannot export the schema.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值