-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix NullPointerException when calling getBBox #1215
Conversation
When calling getBBox inside an onLayout callback of a <Rect />, we were getting a exception thrown: NullPointerException: Attempt to read from field 'float android.graphics.RectF.left' on a null object reference.
Added a fix for a crash when calling |
Thanks for working on this! Have you validated the values from getBBox? Do they make sense to you? I've barely done any testing of that api so it's very much experimental: |
@msand I haven't, but I think because onLayout (and therefore getBBox) is called multiple times in our situation, the last value at least seems ok. But actually the text fixes are causing a lot of crashes. Doesn't trigger the usual red screen in debug mode but adb logcat shows |
@msand just to follow on from the above, it looks like the crashes only occur if calling * Although it would still be nice to understand why this crashes, I would have expected it to return class A extends React.Component {
constructor(props) {
super(props);
this.state = {
textWidth: 0,
}
}
setTextRef = (node) => {
this.textRef = node;
if (node) {
this.onTextLayout();
} else {
this.setState({
textWidth: 0
});
}
}
onTextLayout = () => {
if (this.textRef) {
const {width} = this.textRef.getBBox();
this.setState({
textWidth: width
});
}
}
render() {
return (
<Text ref={this.setTextRef} onLayout={this.onTextLayout}>
<TSpan>Some</TSpan> Text
</Text>
);
}
} |
Have you tried returning new Path() instead of null in the text fixes? |
I am encountering this issue as well. Is the PR waiting on further updates? |
Also fixes for other native methods
I've made a slightly different fix in the develop branch, would you mind testing that? |
I pulled and linked the develop branch to test. For my use case I call getBBox within the onLayout callback of the Text component. The following error is display onscreen: |
At least for me using the develop branch and this code: import React, { Component } from "react";
import { View, StyleSheet } from "react-native";
import { Svg, Text, TSpan } from "react-native-svg";
class A extends React.Component {
constructor(props) {
super(props);
this.state = {
textWidth: 0
};
}
setTextRef = node => {
this.textRef = node;
if (node) {
this.onTextLayout();
} else {
this.setState({
textWidth: 0
});
}
};
onTextLayout = () => {
if (this.textRef) {
const { width } = this.textRef.getBBox();
console.log({ width });
this.setState({
textWidth: width
});
}
};
render() {
return (
<Text y="20" ref={this.setTextRef} onLayout={this.onTextLayout}>
<TSpan>Some</TSpan> Text
</Text>
);
}
}
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<Svg width={"100%"} height={"100%"}>
<A />
</Svg>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
backgroundColor: "#ecf0f1",
padding: 8
}
}); I get this output in android studio:
|
Alternatively you might want something like this: #1231 (comment) |
# [11.0.0](v10.1.0...v11.0.0) (2020-01-18) ### Bug Fixes * compatibility with reanimated color, fixes [#1241](#1241) ([4983766](4983766)) * **android:** NullPointerException when calling getBBox [#1215](#1215) ([3eb82a9](3eb82a9)) * **android:** support animating stroke color ([c5dd62f](c5dd62f)) * **android:** support setting path null ([2d34734](2d34734)) * **ios:** iOS 10.3 renders opaque background when drawRect is defined ([61bc9bd](61bc9bd)), closes [#1252](#1252) * **web:** Allow createElement & unstable_createElement usage ([#1240](#1240)) ([7a23968](7a23968)) * fix(android)!: pivot point for RN transform array syntax ([db682f8](db682f8)) ### BREAKING CHANGES * Makes android specific transform origin adjustments unnecessary / broken. Renders exactly the same as web and ios instead.
Perhaps we can close this now? v11 has been released with the fix |
Summary
When calling
getBBox()
inside anonLayout
callback of a<Rect />
, we were getting a exception thrown:NullPointerException: Attempt to read from field 'float android.graphics.RectF.left' on a null object reference.
Test Plan
What's required for testing (prerequisites)?
What are the steps to reproduce (after prerequisites)?
Compatibility
Checklist
README.md
CHANGELOG.md
example/App.js
)