Skip to content
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

NullPointerException:tempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable(android.content.res.Resources)' #17530

Closed
wxjer opened this issue Jan 11, 2018 · 412 comments
Labels
Bug Platform: Android Android applications. Ran Commands One of our bots successfully processed a command.

Comments

@wxjer
Copy link

wxjer commented Jan 11, 2018

Is this a bug report?

yes

Have you read the Contributing Guidelines?

yes, I am sorry that I cant offer more information about this exception except for this stack trace because the crash report was collected from google analytics, I have no idea to reappear this exception.

Environment

Environment:
OS: macOS Sierra 10.12.6
Node: 8.4.0
Yarn: 0.27.5
npm: 5.4.0
Android Studio: 3.0

Packages: (wanted => installed)
react-native: 0.51.0 => 0.51.0
react: 16.0.0-alpha.12 => 16.0.0-alpha.12

Target Platform: Android (7.1.1)
mobile:MIX 2
android:7.1.1
java.lang.NullPointerException:
tempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable(android.content.res.Resources)' on a null object reference at
android.graphics.drawable.DrawableContainer$DrawableContainerState.getChild(DrawableContainer.java:888) at
android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:466) at
android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:104) at
android.graphics.drawable.Drawable.setState(Drawable.java:735) at
android.graphics.drawable.DrawableWrapper.onStateChange(DrawableWrapper.java:331) at
android.graphics.drawable.Drawable.setState(Drawable.java:735) at
android.graphics.drawable.LayerDrawable.onStateChange(LayerDrawable.java:1488) at
android.graphics.drawable.Drawable.setState(Drawable.java:735) at
android.view.View.drawableStateChanged(View.java:18002) at
android.widget.TextView.drawableStateChanged(TextView.java:4097) at
android.view.View.refreshDrawableState(View.java:18071) at
android.view.View.setPressed(View.java:8543) at
android.view.View.setPressed(View.java:8521) at
android.view.View.onTouchEvent(View.java:11218) at
android.widget.TextView.onTouchEvent(TextView.java:8467) at
com.facebook.react.views.textinput.ReactEditText.onTouchEvent(ReactEditText.java:150)

@KshitijDroid
Copy link

Facing Same Issue.

@yaronlevi
Copy link

Same here RN@0.49.0

com.facebook.react.views.textinput.ReactEditText.onTouchEvent
ReactEditText.java - line 163
java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable(android.content.res.Resources)' on a null object reference
Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable(android.content.res.Resources)' on a null object reference
1
android.graphics.drawable.DrawableContainer$DrawableContainerState.getChild DrawableContainer.java:884
2
android.graphics.drawable.DrawableContainer.selectDrawable DrawableContainer.java:466
3
android.graphics.drawable.StateListDrawable.onStateChange StateListDrawable.java:104
4
android.graphics.drawable.Drawable.setState Drawable.java:736
5
android.graphics.drawable.DrawableWrapper.onStateChange DrawableWrapper.java:331
6
android.graphics.drawable.Drawable.setState Drawable.java:736
7
android.view.View.drawableStateChanged View.java:19223
8
android.widget.TextView.drawableStateChanged TextView.java:4673
9
android.view.View.refreshDrawableState View.java:19292
10
android.view.View.setPressed View.java:9135
11
android.view.View.setPressed View.java:9113
12
android.view.View.onTouchEvent View.java:12357
13
android.widget.TextView.onTouchEvent TextView.java:10095
14
com.facebook.react.views.textinput.ReactEditText.onTouchEvent ReactEditText.java:163

@fatiherdogan01
Copy link

Same Issue

@sospedra
Copy link

Same issue. Same logtrace

@sahil290791
Copy link

Same here on RN 44.0

@thiZguy
Copy link

thiZguy commented Feb 8, 2018

+1 anotha' one

@thiZguy
Copy link

thiZguy commented Feb 8, 2018

Guys restarting the packager and clearing cache worked for me!

@fodjan-philip
Copy link

Same issue for me. I am using RN 0.51.0 and react-native-material-textfield 0.12.0.

@szholdiyarov
Copy link

I am facing this issues as well. The strange thing is that it is very hard to track the error as the stack trace does not contain any useful information. I even do not know what where the error could be.
Will update here If i find the solution

@honzababarik
Copy link

Same error happening on RN 0.52.0.

@react-native-bot
Copy link
Collaborator

Thanks for posting this! It looks like you may not be using the latest version of React Native, v0.53.0, released on January 2018. Can you make sure this issue can still be reproduced in the latest version?

I am going to close this, but please feel free to open a new issue if you are able to confirm that this is still a problem in v0.53.0 or newer.

How to ContributeWhat to Expect from Maintainers

@react-native-bot react-native-bot added Ran Commands One of our bots successfully processed a command. Stale There has been a lack of activity on this issue and it may be closed soon. labels Feb 24, 2018
@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Feb 24, 2018
@gentlee
Copy link

gentlee commented Feb 27, 2018

Same issue on 0.53.3

@yuanboGeng
Copy link

Same issue

@SoundBlaster
Copy link

@react-native-bot Need to reopen issue

@sahil-innostax
Copy link

facing the same issue with Android 8

@rknell
Copy link

rknell commented Mar 9, 2018

Same here. React native 0.53.3, SM-T550, API Level 25 (Android 7.0)

@mhdtouban
Copy link

this issue just popped up with me, React native 0.53.3 - Android 8.1.

@sshkalikov
Copy link

React native 0.53.3, android 8, same issue

@YawenD
Copy link

YawenD commented Mar 20, 2018

Same issue, react-native 0.53.0 and android 8. It strange cause this error seems to appears randomly. I can't find any exact steps to reproduce it.

@jamsch
Copy link
Contributor

jamsch commented Mar 20, 2018

Have a production app running RN 0.54.2 and this seems to affect a few users running Android 8.0.0 on app startup.

selection_024

@m-vdb
Copy link

m-vdb commented Mar 26, 2018

Why is this issue closed? Some of us are facing this issue in production environment, so "restarting the packager" is not an acceptable solution.

@JCMais
Copy link

JCMais commented Mar 28, 2018

This issue needs to be reopened since it affects current version.

@jgcmarins
Copy link

jgcmarins commented Mar 28, 2018

I am also facing stack traces that might be related:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable(android.content.res.Resources)' on a null object reference
       at android.graphics.drawable.DrawableContainer$DrawableContainerState.createAllFutures(DrawableContainer.java:875)
       at android.graphics.drawable.DrawableContainer$DrawableContainerState.getOpacity(DrawableContainer.java:1158)
       at android.graphics.drawable.DrawableContainer.getOpacity(DrawableContainer.java:433)
       at android.graphics.drawable.InsetDrawable.getOpacity(InsetDrawable.java:258)
       at android.view.View.computeOpaqueFlags(View.java:16791)
       at android.view.View.setBackgroundDrawable(View.java:21710)
       at android.view.View.setBackground(View.java:21603)
       at android.view.View.<init>(View.java:5547)
       at android.widget.TextView.<init>(TextView.java:1135)
       at android.widget.EditText.<init>(EditText.java:107)
       at android.widget.EditText.<init>(EditText.java:103)
       at android.widget.EditText.<init>(EditText.java:99)
       at android.widget.EditText.<init>(EditText.java:95)
       at com.facebook.react.views.textinput.ReactEditText.<init>(ReactEditText.java:92)
       at com.facebook.react.views.textinput.ReactTextInputManager.createViewInstance(ReactTextInputManager.java:94)
       at com.facebook.react.views.textinput.ReactTextInputManager.createViewInstance(ReactTextInputManager.java:65)
       at com.facebook.react.uimanager.ViewManager.createView(ViewManager.java:46)
       at com.facebook.react.uimanager.NativeViewHierarchyManager.createView(NativeViewHierarchyManager.java:218)
       at com.facebook.react.uimanager.UIViewOperationQueue$CreateViewOperation.execute(UIViewOperationQueue.java:150)
       at com.facebook.react.uimanager.UIViewOperationQueue$DispatchUIFrameCallback.dispatchPendingNonBatchedOperations(UIViewOperationQueue.java:923)
       at com.facebook.react.uimanager.UIViewOperationQueue$DispatchUIFrameCallback.doFrameGuarded(UIViewOperationQueue.java:895)
       at com.facebook.react.uimanager.GuardedFrameCallback.doFrame(GuardedFrameCallback.java:31)
       at com.facebook.react.modules.core.ReactChoreographer$ReactChoreographerDispatcher.doFrame(ReactChoreographer.java:136)
       at com.facebook.react.modules.core.ChoreographerCompat$FrameCallback$1.doFrame(ChoreographerCompat.java:107)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:909)
       at android.view.Choreographer.doCallbacks(Choreographer.java:723)
       at android.view.Choreographer.doFrame(Choreographer.java:655)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
       at android.os.Handler.handleCallback(Handler.java:789)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6938)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.DrawableContainer$DrawableContainerState$ConstantStateFuture.get(android.graphics.drawable.DrawableContainer$DrawableContainerState)' on a null object reference
       at android.graphics.drawable.DrawableContainer$DrawableContainerState.getChild(DrawableContainer.java:823)
       at android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:452)
       at android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:104)
       at android.graphics.drawable.Drawable.setState(Drawable.java:680)
       at android.graphics.drawable.DrawableWrapper.onStateChange(DrawableWrapper.java:279)
       at android.graphics.drawable.Drawable.setState(Drawable.java:680)
       at android.graphics.drawable.LayerDrawable.onStateChange(LayerDrawable.java:1381)
       at android.graphics.drawable.Drawable.setState(Drawable.java:680)
       at android.view.View.drawableStateChanged(View.java:17003)
       at android.widget.TextView.drawableStateChanged(TextView.java:3984)
       at android.view.View.refreshDrawableState(View.java:17067)
       at android.view.View.setPressed(View.java:7914)
       at android.view.View.setPressed(View.java:7892)
       at android.view.View.access$2900(View.java:708)
       at android.view.View$CheckForTap.run(View.java:21187)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5451)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

@grabbou can we re-open this issue?

@j-mendez
Copy link

j-mendez commented Mar 28, 2018

Same issue here android 8.0 api 26

@lukebarnard1
Copy link

lukebarnard1 commented Apr 2, 2018

Same on 0.54.2.

I've narrowed it down to a <TextInput> being rendered within in a FlatList. When I replace this with a <Text>, the issue disappears.

@radko93
Copy link
Contributor

radko93 commented Apr 10, 2018

Please reproduce with the latest version available now and I will reopen this.

@douglasjunior
Copy link

Guys, what fixes the problem is not the React Native version update, but a change in native styles, which can be viewed here: https://react-native-community.github.io/upgrade-helper/?from=0.66.0&to=0.67.4

  • android/app/src/main/res/drawable/rn_edit_text_material.xml
  • android/app/src/main/res/values/styles.xml

image

So, if you update your RN version, but not apply these new styles, the problem will not go away.

@namnm
Copy link

namnm commented Jun 30, 2022

Thanks but a lot of people mentioned above:

  • Updated RN version
  • Applied new styles

This error still happens sometimes. It happens less frequently than before, but still happening.

@rafaelnco
Copy link

hello @namnm i have just ran into this same problem, the updates from RN upgrade helper + update RN version wont work, i am even thinking about implementing my own TextInput JS version to avoid rendering RNTextInput directly, do you think this is reliable? i am having some issues on styling RNTextInput but for a quick test i was able to shut down the crash

@rafaelnco
Copy link

hey guys i have been able to cut off this crash on JS side, do you think it is reliable? i am on RN 0.62

@glenn-axsy
Copy link

@rafaelnco does it still appear when you use the testing component from #17530 (comment) above?

@rafaelnco
Copy link

hey @glenn-axsy sorry for the delay! im really sorry i havent had the time to test it using the testing component youve mentioned..

@MuhammadRafeh
Copy link

Please avoid opacity to textinputs. It may help you avoid this crash. But If it's necessary for you guys to use opacity then give opacity to view and make it as a wrapper for textinput.

@hrishi2430
Copy link

Guys, what fixes the problem is not the React Native version update, but a change in native styles, which can be viewed here: https://react-native-community.github.io/upgrade-helper/?from=0.66.0&to=0.67.4

  • android/app/src/main/res/drawable/rn_edit_text_material.xml
  • android/app/src/main/res/values/styles.xml

image

So, if you update your RN version, but not apply these new styles, the problem will not go away.

thanks @douglasjunior it works

@GaelCO
Copy link

GaelCO commented Oct 7, 2022

@hrishi2430 This crash occurs even with the style change. Just less often.

@AsadAlihp
Copy link

Error still exits

@jdicami
Copy link

jdicami commented Oct 25, 2022

Why is this closed, it's still an issue on production builds even on RN 0.70.3

@GunjanRajTiwari
Copy link

Still getting this issue in 0.70.5

@Developeranees
Copy link

same issue

@Rosdel
Copy link

Rosdel commented Jan 16, 2023

Guys some clear way forward.... am getting lost

@htthecool
Copy link

I was getting the same error.
Android version: 10
RN: 0.70.6

The bug was because an external library - react-native-google-places-autocomplete (v2.5.1)

Implementation -> I had a Modal that allowed user to input address for places-autocomplete. Tap on "input" should open keyboard. But, because of wrong implementation the keyboard was not opening (hence the drawable method unavailable).

The solution was to fix JS code -> adding "keyboardShouldPersistTaps" worked.

I am assuming that some of you might be facing similar issue.

I hope this helps

@vishuk570
Copy link

Add the flowing line of code into /android/app/src/main/res/values/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
<item name="android:editTextBackground">@android:color/transparent</item> <-- Add this
 ... 
</style>

Hope this will work.

@glenn-axsy
Copy link

@vishuk570 yes that worked for us.

If anyone has trouble reproducing this issue, or want to confirm they have fixed it, you might like to try my testing component that I detailed in an earlier comment.

drewjhart added a commit to rightoneducation/righton-app that referenced this issue Mar 10, 2023
facebook/react-native#17530

and crash 2:
disable hermes for a better crash report
@jcdhlzq
Copy link
Contributor

jcdhlzq commented Apr 4, 2023

I was able to create a Minimum Reproducible Example. The issue is caused from the TextInput key prop.

Re-rendering a high number of TextInput components with key prop will cause the runtime error. The number of TextInputs seems crucial to trigger the crash.

   // Does not crash after testing for 5 minutes
    for (let i = 0; i < 5; i++) { ..
   // Crashes after 20 seconds
    for (let i = 0; i < 100; i++) { ..
   // Crashes after 1 second
    for (let i = 0; i < 1000; i++) { ..

The crash does not reproduce if you remove the TextInput key prop. I will investigate and try to fix it. Run this example to reproduce it.

import * as React from "react";
import { View, TextInput } from "react-native";

class App extends React.Component{
  state = { startKey: 0 };
  componentDidMount() {
    this.interval = setInterval(this.updateKey, 3000);
  }

  componentWillUnmount() {
    clearInterval(this.interval);
  }

  updateKey = () => {
    this.setState({
      startKey: this.state.startKey + 100
    });
  };

  render() {
    const textInputs = [];

    for (let i = 0; i < 100; i++) {
      const key = (this.state.startKey + i).toString();
      console.log("key", key);
      // REMOVE KEY PROP TO FIX THIS
      textInputs.push(
        <TextInput key={key} />
      );
    }

    return (
      <View>
        { textInputs }
      </View>
    );
  }
}

export default App;

The same issue can be reproduced with a FlatList, if your data prop includes a key, index or id like this [{ key: "1"}, {key: "2"}, .. {key: "5000"}], automatically the key will be used for your List and causes the Runtime. Removing those keys from the data array fixes the error and removes automatic caching.

* and as the react key to track item re-ordering. The default extractor checks `item.key`, then
* falls back to using the index, like React does.

import * as React from "react";
import { Text, FlatList, View, TextInput } from "react-native";

class App extends React.Component{
  state = { startKey: 0 };
  componentDidMount() {
    this.interval = setInterval(this.updateKey, 3000);
  }
  
  componentWillUnmount() {
    clearInterval(this.interval);
  }

  updateKey = () => {
    this.setState({
      startKey: this.state.startKey + 100
    });
  };

  render() {
    const textInputs = [];

    for (let i = 0; i < 5000; i++) {
      const notKey = (this.state.startKey + i).toString();
      console.log("key", notKey);
      // REMOVE KEY OR ID FIELD TO FIX THIS
      // replacing notKey with key will trigger the runtime
      textInputs.push(
        { notKey }
      );
    }

    return (
      <FlatList
        data={textInputs}
        renderItem={({item}) => <TextInput value={"testing"} />}
      />
    );
  }
}

export default App;

I find that it is also useful to change StateList like this:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<inset xmlns:android="http://schemas.android.com/apk/res/android"
  android:insetLeft="@dimen/abc_edit_text_inset_horizontal_material"
  android:insetRight="@dimen/abc_edit_text_inset_horizontal_material"
  android:insetTop="@dimen/abc_edit_text_inset_top_material"
  android:insetBottom="@dimen/abc_edit_text_inset_bottom_material">

  <selector>
    <!--
      This file is a copy of abc_edit_text_material (https://bit.ly/3k8fX7I).
      The item below with state_pressed="false" and state_focused="false" causes a NullPointerException.
      NullPointerException:tempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable(android.content.res.Resources)'
      <item android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/abc_textfield_default_mtrl_alpha"/>
      For more info, see https://bit.ly/3CdLStv (react-native/pull/29452) and https://bit.ly/3nxOMoR.
    -->
    <item android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/abc_textfield_default_mtrl_alpha"/>
    <item android:state_enabled="false" android:drawable="@drawable/abc_textfield_default_mtrl_alpha"/>
    <item android:drawable="@drawable/abc_textfield_activated_mtrl_alpha"/>
  </selector>

</inset>

it just puts the line <item android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/abc_textfield_default_mtrl_alpha"/> on the first place, then the problem could not be reproduced.

maybe the root cause of this problem is related to the order of StateList, but i didn't get the evidence.

Vittor-Javidan added a commit to Vittor-Javidan/EarthLog that referenced this issue Oct 21, 2023
- Removed undeline decoration for carousel buttons. Due the possible fatar error described on this issue: facebook/react-native#17530
- This bugged already happenend once in development, and it blocks permanently access to app after occurs once, no matter how many times the app is restarted.
@vanBerloDevelopments
Copy link

vanBerloDevelopments commented Nov 3, 2023

I tried many things on this page and also used a similar approach on reproducing the crash as @jcdhlzq.

I found that removing the key property reduced the amount of crashes but they were still there. What finally fixed it for me was adding these lines to the main/res/values/styles.xml

In your styles, try setting explicit background colors

<item name="android:editTextStyle">@style/MyEditTextStyle</item>

<style name="MyEditTextStyle" parent="Widget.AppCompat.EditText">
    <item name="android:background">#FFFFFF</item>
</style>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Platform: Android Android applications. Ran Commands One of our bots successfully processed a command.
Projects
None yet
Development

Successfully merging a pull request may close this issue.