我已经做了很多 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)
提前致谢