1

我已经做了很多 android,但对 java 8 来说是新手。我做了一个 BehaviourSubject< Map< String,String>>。

我在上面放了一个可观察的。在演示者类中,我将 BehaviourSbuject 设置为可观察的,并且一旦进行 onClick,onNext 就会调用它。但它正在抛出 rx.exceptions.OnErrorNotImplementedException。

我不明白为什么它会抛出这个错误,因为我已经参考了这个http://reactivex.io/RxJava/javadoc/index.html?rx/exceptions/OnErrorNotImplementedException.html但据此我只在 onNext 调用可观察到仍然出现此错误。

MainScreenView 是一个由 MainScreenFragment 实现的接口。

演示者类

public class MainScreenPresenter {
public MainScreenPresenter(MainScreenView view, MainScreenModel model) {
    Log.i("MainScreenPresenter", "default constructor");
    view.addPlace().subscribe(place -> model.place(place));
    }
}

MainScreenFragment 类

public class MainScreenFragment extends Fragment implements MainScreenView, View.OnClickListener {

ArrayList<Place> places;
View mainScreenRootFragment;
Button addPlace;
TextView venueDate;
Place newPlace;
BehaviorSubject<Map<String, String>> placeAdded = BehaviorSubject.create();


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mainScreenRootFragment = inflater.inflate(R.layout.main_screen_fragment, container);
    Log.i("MainScreenFragment", "onCreateView");
    setRetainInstance(true);
    return mainScreenRootFragment;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Log.i("MainScreenFragment", "onViewCreated");
    initializeViews(view);
    setEventsForViews();
}

private void setEventsForViews() {
    addPlace.setOnClickListener(this);
}

private void initializeViews(View view) {
    addPlace = (Button) view.findViewById(R.id.add_place);
    venueDate = (TextView) view.findViewById(R.id.venue_date);
    places = new ArrayList<>();
}

@Override
public Observable<Map<String, String>> addPlace() {
    Log.i("MainScreenFragment", "addPlace");
    return placeAdded.asObservable();
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.add_place) {
        Log.i("MainScreenFragment", "add_place button clicked");
        placeData();
        Log.i("MainScreenFragment", "after onNext()");
    } else if (v.getId() == R.id.rv) {

    }
}

private void placeData() {
    Log.i("MainScreenFragment", "data is mapping in placeData()");
    Map<String, String> map = new HashMap<>();

    map.put("placeName", "Lonavala");
    map.put("placeDate", "20/5/2015");
    placeAdded.onNext(map);
    }
}

错误

07-10 10:14:25.414    2754-2754/com.example.vikky.hisab W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41703d58)
07-10 10:14:25.424    2754-2754/com.example.vikky.hisab E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.vikky.hisab, PID: 2754
rx.exceptions.OnErrorNotImplementedException
        at rx.Observable$31.onError(Observable.java:7204)
        at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:127)
        at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:96)
        at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:111)
        at rx.internal.operators.NotificationLite.accept(NotificationLite.java:150)
        at rx.subjects.SubjectSubscriptionManager$SubjectObserver.emitNext(SubjectSubscriptionManager.java:254)
        at rx.subjects.BehaviorSubject.onNext(BehaviorSubject.java:166)
        at com.example.vikky.hisab.MainScreenFragment.placeData(MainScreenFragment.java:115)
        at com.example.vikky.hisab.MainScreenFragment.onClick(MainScreenFragment.java:100)
        at android.view.View.performClick(View.java:4444)
        at android.view.View$PerformClick.run(View.java:18440)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5050)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.vikky.hisab.MainScreenPresenter.lambda$new$5(MainScreenPresenter.java:11)
        at com.example.vikky.hisab.MainScreenPresenter.access$lambda$0(MainScreenPresenter.java)
        at com.example.vikky.hisab.MainScreenPresenter$$Lambda$1.call(Unknown Source)
        at rx.Observable$31.onNext(Observable.java:7209)
        at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:104)

提前致谢

4

1 回答 1

1

在我看来,在您的OnNext处理程序中,您正在抛出一个NullPointerException. 我的猜测是,然后 RxJava 正在尝试捕获这个并将异常发布到OnError您尚未提供的处理程序。然后将其升级为OnErrorNotImplementedException.

所以这里要做两件事:

  1. 始终有一个处理程序的好习惯OnError。如果您不这样做,则无法在异步世界中捕获异常。
  2. 我的猜测是model空值,所以调用model.place(place)是抛出 NPE 的东西。也许它是你的地方方法,但堆栈跟踪似乎指向为model空。
于 2015-07-10T12:55:19.913 回答