0

我正在学习如何在 Android 中处理和使用函数式编程。所以我开发了下面的代码。我想将 HandlerThread 视为可观察的,但是当我尝试从 .map() 运算符调用 .start() 时,我收到以下错误:

    no instances of type variable(s) R exists so that  void conforms to R

请让我知道为什么我会收到此错误以及如何解决它。

代码

public Single<HandlerThread> getObsInitializedHandlerThread() {
    this.mMyHandlerThread = new MyHandlerThread(NAME_MY_HANDLER_THREAD);
    return Single.just(this.mMyHandlerThread);
}

@Override
protected void onResume() {
    super.onResume();
    String TAG_LOG = ActMain.TAG_LOG + "." + "onResume()";
    Log.v(TAG_LOG, ":");

    this.getObsInitializedHandlerThread()
            .map(mMyHandlerThread -> mMyHandlerThread.start());
}

private class MyHandlerThread extends HandlerThread {

    public MyHandlerThread(String name) {
        super(name);
        String TAG_LOG = ActMain.class.getSimpleName() + "." + "MyHandlerThread() Constructor";
        Log.v(TAG_LOG, ":");
    }

    @Override
    protected void onLooperPrepared() {
        super.onLooperPrepared();
        String TAG_LOG = ActMain.class.getSimpleName() + "." + onLoopPrepared()";
        Log.v(TAG_LOG, ":");
    }
}
4

1 回答 1

0

在地图中,你总是需要返回一个值(T)你不能返回一个 void 所以你可以试试

 this.getObsInitializedHandlerThread()
        .map(mMyHandlerThread ->{
 mMyHandlerThread.start();
return mMyHandlerThread;
});
于 2018-12-21T13:32:35.137 回答