为什么 'subscribe' 函数没有注册? RxJava Android 工作室
Why is 'subscribe' function not registering? RxJava Android Studio
作为项目的一部分,我正在为启动画面编写代码,但是当我尝试使用 RxJava 时,所有其他功能都可以正常工作,直到我到达 'subscribe',但它没有注册,所以我我想知道我是否遗漏了一个我没有导入的库或其他东西?
我在尝试 运行 时收到的错误消息是:“无法解析方法 'subscribe'。我试过询问教程创建者,并在 Whosebug 上查看有关 'subscribe' 函数的最重要的相关问题,甚至查看 Completable 库中的完整方法列表,但我找不到解决方案,所以我真的很绝望,请帮助
这些是我的依赖项:
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.20'
我的代码:
package codebymech.fyprideshareapp;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Notification;
import android.os.Bundle;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;
import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
public class Activity_SplashScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
delay_Splash();
}
private void delay_Splash() {
Completable.timer(delay: 5, TimeUnit.SECONDS,
AndroidSchedulers.mainThread())
.subscribe(new Action() {
@Override
public void run() throws Exception {
Toast.makeText(Activity_SplashScreen.this, "!! Splash Screen Finished", Toast.LENGTH_SHORT).show();
}
});
}
}
部分教程代码来源:
https://youtu.be/144TuYxEu2M?t=489
您必须删除 delay:
。这在 Java 中是不可能的。如果你看视频,你会在给定的方法中看到一个 delay:
,但它是由 IDE (IntelliJ) 添加的,以便给你一个提示,你当前正在编辑哪个参数。
导入
import io.reactivex.Completable;
import io.reactivex.functions.Action;
import io.reactivex.schedulers.Schedulers;
Java8
Completable.timer(5, TimeUnit.SECONDS,
Schedulers.trampoline())
.subscribe(() -> {
System.out.println("x");
});
Java7
Completable.timer(5, TimeUnit.SECONDS,
Schedulers.trampoline())
.subscribe(new Action() {
@Override
public void run() throws Exception {
System.out.println("x");
}
});
作为项目的一部分,我正在为启动画面编写代码,但是当我尝试使用 RxJava 时,所有其他功能都可以正常工作,直到我到达 'subscribe',但它没有注册,所以我我想知道我是否遗漏了一个我没有导入的库或其他东西?
我在尝试 运行 时收到的错误消息是:“无法解析方法 'subscribe'。我试过询问教程创建者,并在 Whosebug 上查看有关 'subscribe' 函数的最重要的相关问题,甚至查看 Completable 库中的完整方法列表,但我找不到解决方案,所以我真的很绝望,请帮助
这些是我的依赖项:
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.20'
我的代码:
package codebymech.fyprideshareapp;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Notification;
import android.os.Bundle;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;
import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
public class Activity_SplashScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
delay_Splash();
}
private void delay_Splash() {
Completable.timer(delay: 5, TimeUnit.SECONDS,
AndroidSchedulers.mainThread())
.subscribe(new Action() {
@Override
public void run() throws Exception {
Toast.makeText(Activity_SplashScreen.this, "!! Splash Screen Finished", Toast.LENGTH_SHORT).show();
}
});
}
}
部分教程代码来源: https://youtu.be/144TuYxEu2M?t=489
您必须删除 delay:
。这在 Java 中是不可能的。如果你看视频,你会在给定的方法中看到一个 delay:
,但它是由 IDE (IntelliJ) 添加的,以便给你一个提示,你当前正在编辑哪个参数。
导入
import io.reactivex.Completable;
import io.reactivex.functions.Action;
import io.reactivex.schedulers.Schedulers;
Java8
Completable.timer(5, TimeUnit.SECONDS,
Schedulers.trampoline())
.subscribe(() -> {
System.out.println("x");
});
Java7
Completable.timer(5, TimeUnit.SECONDS,
Schedulers.trampoline())
.subscribe(new Action() {
@Override
public void run() throws Exception {
System.out.println("x");
}
});