java - 将类型转换器与 Room 一起使用时出现 StackOverFlow 错误

标签 java android android-room

我正在与 TheMovieDatabase API 交互,发现 here .

我正在尝试提取流行度字段,该字段的对象类型为Number

Room 需要一个用于该对象的类型转换器,我已将其集成在下面:

public class NumberConverter {

@TypeConverter
public static Number toNumber(Integer integer){
    return integer == null ? null : toNumber(integer); }


@TypeConverter
public static Integer toInt(Number number){
    return number == null ? null : number.intValue();
  }


}

当我加载应用程序时,我立即收到引用此行的 StackOverFlow 错误:

        return number == null ? null : number.intValue();

注意:我还使用 Executors 来异步处理读写:

public class AppExecutors{

//for Singleton Instantiation
private static final Object LOCK = new Object();
private static AppExecutors sInstance;
private final Executor diskIO;
private final Executor mainThread;
private final Executor networkIO;

public AppExecutors(Executor diskIO, Executor mainThread, Executor networkIO) {
    this.diskIO = diskIO;
    this.mainThread = mainThread;
    this.networkIO = networkIO;
}


public static AppExecutors getsInstance(){
    if (sInstance == null){
        synchronized (LOCK){
            sInstance = new AppExecutors(Executors.newSingleThreadExecutor(),
                    Executors.newFixedThreadPool(3),
                    new MainThreadExecutor());
        }
    }
    return sInstance;
};

public Executor diskIO(){return diskIO;};
public Executor mainThread(){return mainThread;}
public Executor netWorkIO(){return networkIO;}

private static class MainThreadExecutor implements Executor{

    private android.os.Handler mainThreadHandler = new android.os.Handler(Looper.getMainLooper());

    @Override
    public void execute(@NonNull Runnable runnable) {
        mainThreadHandler.post(runnable);
    }
   }



}

编辑:引用TheMovieDatabase文档

最佳答案

你能在你的代码中检查一下这个函数吗:

@TypeConverter 公共(public)静态数字toNumber(整数整数){ 返回整数 == null ? null : toNumber(整数); }

这里发生了无限的递归,也许这就是您收到StackOverflowError的原因。

关于java - 将类型转换器与 Room 一起使用时出现 StackOverFlow 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51341668/

相关文章:

java - 当 RecyclerView 中有超过 5 个项目时,Switch 会更改其行为

java - 用于自定义对象的 Android 房间类型转换器

java - 从 json 到 json 立即崩溃

java - 动态(以编程方式)创建 View 时, View 在卡片 View 中重叠

java - 移动一个元素而不移动其他元素 [Android ADT]

java - 向下抛投时不同级别的抛投性能

android - 错误 :error: 'drawable/delete.png' is incompatible with attribute android:background (attr) reference|color

Android Room Dao : Order By CASE not working

java - 在 Android 的单元测试中覆盖 SLF4J 实现

android - 在 Android 中获取错误反馈