NullPointerException:tempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable(android.content.res.Resources)'
Problem
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.drawableS
Error Output
exception except for this stack trace because the crash report was collected from google analytics, I have no idea to reappear this exception.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix NullPointerException in Drawable Handling
The NullPointerException occurs when the application attempts to invoke a method on a Drawable object that is not properly initialized. This can happen if the Drawable resource is missing or if the Drawable state is not set correctly in the StateListDrawable or LayerDrawable. In this case, the DrawableContainer is trying to access a child Drawable that is null.
Awaiting Verification
Be the first to verify this fix
- 1
Check Drawable Resources
Ensure that all Drawable resources referenced in your layout files or code are present and correctly defined. Missing resources can lead to null references.
bashCheck res/drawable/ for missing files. - 2
Initialize Drawable Properly
Make sure that any Drawable objects are initialized before being used. Use a fallback Drawable if the primary one is null.
javaDrawable myDrawable = getResources().getDrawable(R.drawable.my_drawable); if (myDrawable == null) { myDrawable = getResources().getDrawable(R.drawable.fallback_drawable); } - 3
Handle State Changes Safely
When changing states in StateListDrawable or LayerDrawable, ensure that the Drawable being set is not null. Add checks to prevent state changes on null Drawables.
javaif (myDrawable != null) { myDrawable.setState(newState); } - 4
Update Dependencies
Ensure that you are using the latest stable versions of React Native and related libraries, as bugs are often fixed in newer releases.
bashnpm install react-native@latest - 5
Test on Multiple Devices
After making the changes, test the application on multiple devices and Android versions to ensure that the issue is resolved and does not occur again.
bashUse Android Emulator and physical devices for testing.
Validation
Confirm the fix by running the application and checking for any occurrences of the NullPointerException in the logs. Additionally, perform UI tests to ensure that all Drawable states are functioning as expected without crashes.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep