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

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

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.

猜你喜欢

转载自blog.csdn.net/qq_45254908/article/details/107831809