question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling measure on the element which is off the screen causing crash on Android

See original GitHub issue

Description

If the element is off the screen (user can’t see it) on iOS we can get its measurements, on Android it causing the app to crash.

Screenshots

Steps To Reproduce

  1. Run provided example
  2. Wait 1 second. It will try to measure the element which is off the screen

Expected behavior

Should work just like on iOS – should return values off the screen

Actual behavior

Crashes

Snack or minimal code example

import React from 'react';
import { useEffect } from 'react';
import {
  FlatList,
  TapGestureHandler,
} from 'react-native-gesture-handler';
import Animated, {
  useAnimatedRef,
  useAnimatedGestureHandler,
  measure,
  runOnUI,
} from 'react-native-reanimated';

function Block({ index }) {
  const ref = useAnimatedRef();

  const handler = useAnimatedGestureHandler({
    onFinish: () => {
      console.log(measure(ref));
    },
  });

  useEffect(() => {
    if (index === 9) {
      setTimeout(
        () =>
          runOnUI(() => {
            'worklet';

            console.log(measure(ref));
          })(),
        1000,
      );
    }
  }, []);

  return (
    <TapGestureHandler onGestureEvent={handler}>
      <Animated.View
        ref={ref}
        style={{
          height: 200,
          marginBottom: 16,
          backgroundColor: 'red',
        }}
      />
    </TapGestureHandler>
  );
}

function Navigation() {
  return (
    <FlatList
      data={Array.from({ length: 10 }).map((item, index) => index)}
      renderItem={({ index }) => <Block index={index} />}
    />
  );
}

Package versions

  • React: 16.13.1
  • React Native: 0.63.2
  • React Native Reanimated: 2.0.0-alpha.7
  • NodeJS: 12.16.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
giovanijfccommented, Mar 30, 2021

Hello, forgive me for the english of the google translator.

It seems to me that this is a rendering problem on android, the measure tries to get the measurements before the view is even rendered on the screen, I got a good result with the code below.

Even with this result, my team and I chose not to use measure less on android until we have a definitive correction, because this palliative solution that is found below may not work in 100% of cases.

const [alreadyRender, setAlreadyRender] = useState<boolean>(false)

useEffect(() => {
    if (!alreadyRender) return
    runOnUI(() => {
      'worklet'
      console.log(Platform.OS, measure(ref))
    })()
}, [alreadyRender])

<Container onLayout={() => setAlreadyRender(true)}>
1reaction
terrysahaidakcommented, Nov 9, 2020

Hi there, thanks for your reply, it seems like I had some caching issues on Android because now try-catch actually catches the error just like it’s on iOS.

I will let you know if I find any other issues regarding that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Crashes - Android Developers
An Android app crashes whenever there's an unexpected exit caused by an unhandled exception or signal. An app that is written using Java...
Read more >
RecyclerView: Inconsistency detected. Invalid item position
Scroll recycler view. So I assume this crash happens when you remove items from the list and scroll without calling notify methods.
Read more >
My App Crashed, Now What? - RayWenderlich.com
You'll find a project called CrashGallery. Crash Gallery main screen. The project shows some common scenarios that cause your app to crash.
Read more >
How to Diagnose App Issues Using Crash Logs - Papertrail
With our device connected via USB before the crash, Android Studio's Logcat feature will display information about our app and the crash. To...
Read more >
How to fix Premiere Pro crash issues - Adobe Support
Check if any instance of the app is running in the background. If so, force quit it using Activity Monitor (macOS) or Task...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found