Skip to content

Commit 291c01f

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Fix NullPointerException when emiting event using UIManagerModule
Reviewed By: achen1 Differential Revision: D8321766 fbshipit-source-id: ae5052c83f46e08d540b90bf5b71c68f354c566d
1 parent e5aa5b7 commit 291c01f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

Diff for: ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventDispatcher.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,14 @@ public int compare(Event lhs, Event rhs) {
9696

9797
private Event[] mEventsToDispatch = new Event[16];
9898
private int mEventsToDispatchSize = 0;
99-
private volatile @Nullable ReactEventEmitter mReactEventEmitter = new ReactEventEmitter();
99+
private volatile ReactEventEmitter mReactEventEmitter;
100100
private short mNextEventTypeId = 0;
101101
private volatile boolean mHasDispatchScheduled = false;
102102

103103
public EventDispatcher(ReactApplicationContext reactContext) {
104104
mReactContext = reactContext;
105105
mReactContext.addLifecycleEventListener(this);
106+
mReactEventEmitter = new ReactEventEmitter(mReactContext);
106107
}
107108

108109
/**

Diff for: ReactAndroid/src/main/java/com/facebook/react/uimanager/events/ReactEventEmitter.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.util.Log;
1313
import android.util.SparseArray;
1414
import com.facebook.infer.annotation.Assertions;
15+
import com.facebook.react.bridge.ReactApplicationContext;
1516
import com.facebook.react.bridge.WritableArray;
1617
import com.facebook.react.bridge.WritableMap;
1718
import com.facebook.react.uimanager.common.UIManagerType;
@@ -24,8 +25,11 @@ public class ReactEventEmitter implements RCTEventEmitter {
2425

2526
private static final String TAG = ReactEventEmitter.class.getSimpleName();
2627
private final SparseArray<RCTEventEmitter> mEventEmitters = new SparseArray<>();
28+
private final ReactApplicationContext mReactContext;
2729

28-
public ReactEventEmitter() { }
30+
public ReactEventEmitter(ReactApplicationContext reactContext) {
31+
mReactContext = reactContext;
32+
}
2933

3034
public void register(@UIManagerType int uiManagerType, RCTEventEmitter eventEmitter) {
3135
mEventEmitters.put(uiManagerType, eventEmitter);
@@ -48,12 +52,16 @@ public void receiveTouches(
4852

4953
Assertions.assertCondition(touches.size() > 0);
5054

51-
int targetReactTag = touches.getMap(0).getInt(TARGET_KEY);
52-
getEventEmitter(targetReactTag).receiveTouches(eventName, touches, changedIndices);
55+
int reactTag = touches.getMap(0).getInt(TARGET_KEY);
56+
getEventEmitter(reactTag).receiveTouches(eventName, touches, changedIndices);
5357
}
5458

5559
private RCTEventEmitter getEventEmitter(int reactTag) {
5660
int type = ViewUtil.getUIManagerType(reactTag);
57-
return mEventEmitters.get(type);
61+
RCTEventEmitter eventEmitter = mEventEmitters.get(type);
62+
if (eventEmitter == null) {
63+
eventEmitter = mReactContext.getJSModule(RCTEventEmitter.class);
64+
}
65+
return eventEmitter;
5866
}
5967
}

0 commit comments

Comments
 (0)