与房间一起使用类型转换器时的StackOverFlow错误

时间:2018-07-14 17:14:34

标签: java android android-room

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

我正在尝试提取对象类型为Number的Popularity字段。

房间需要此对象的类型转换器,我在下面进行了集成:

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文档

1 个答案:

答案 0 :(得分:1)

您可以在代码中检查此功能:

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

这里有无限的recursion,也许这就是为什么您得到StackOverflowError的原因。