fix(android): sendComponentEvent with view==null crash

This commit is contained in:
maxli
2023-09-14 19:22:43 +08:00
committed by siguangli
parent 15b16a88cf
commit d73dc28768
2 changed files with 17 additions and 9 deletions
@@ -57,12 +57,17 @@ public class ViewPagerPageChangeListener implements ViewPager.OnPageChangeListen
params.put(PAGE_ITEM_POSITION, position);
EventUtils.sendComponentEvent(mPager, EventUtils.EVENT_PAGE_SELECTED, params);
View currView = mPager.getViewFromAdapter(mCurrPageIndex);
params.put(PAGE_ITEM_POSITION, mCurrPageIndex);
EventUtils.sendComponentEvent(currView, EventUtils.EVENT_PAGE_ITEM_WILL_APPEAR, params);
if (currView != null) {
params.put(PAGE_ITEM_POSITION, mCurrPageIndex);
EventUtils.sendComponentEvent(currView, EventUtils.EVENT_PAGE_ITEM_WILL_APPEAR,
params);
}
View lastView = mPager.getViewFromAdapter(mLastPageIndex);
params.put(PAGE_ITEM_POSITION, mLastPageIndex);
EventUtils.sendComponentEvent(lastView, EventUtils.EVENT_PAGE_ITEM_WILL_DISAPPEAR,
params);
if (lastView != null) {
params.put(PAGE_ITEM_POSITION, mLastPageIndex);
EventUtils.sendComponentEvent(lastView, EventUtils.EVENT_PAGE_ITEM_WILL_DISAPPEAR,
params);
}
}
}
@@ -135,11 +135,14 @@ public class EventUtils {
* @param params event extra params object
*/
@MainThread
public static void sendComponentEvent(@NonNull View view, @NonNull String eventName,
public static void sendComponentEvent(@Nullable View view, @NonNull String eventName,
@Nullable Object params) {
// UI component event default disable capture and bubble phase,
// can not enable both in native and js.
send(view, view.getId(), eventName, params, false, false, EventType.EVENT_TYPE_COMPONENT);
if (view != null) {
// UI component event default disable capture and bubble phase,
// can not enable both in native and js.
send(view, view.getId(), eventName, params, false, false,
EventType.EVENT_TYPE_COMPONENT);
}
}
@MainThread