NullPointerException in ContextWrapper.getResources ()

I really don’t know what happens with the Android application that I am creating. At some point in my development, I started tearing it apart as soon as it starts in the emulator, with the message “Foo stopped”. Here is the error log if anyone can help me:

07-05 21:13:30.063 7647-7647/root.fazerumsom E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: root.fazerumsom, PID: 7647 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{root.fazerumsom/root.fazerumsom.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.content.ContextWrapper.getResources(ContextWrapper.java:89) at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78) at root.fazerumsom.MainActivity.<init>(MainActivity.java:31) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1208) at android.app.Instrumentation.newActivity(Instrumentation.java:1061) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)             at android.app.ActivityThread.access$800(ActivityThread.java:135)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5001)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)             at dalvik.system.NativeStart.main(Native Method) 
+6
source share
2 answers

You call getResources() from the field initializer. Sort of

 private String mStr = getResources().getString(...); 

(or using resources or other resources).

This is not true because the context has not yet been configured. Move this assignment inside the onCreate() method.

+13
source

The java.lang.NullPointerException checked exception is thrown when you try to use an object without a reference, in simple words, this means: you are trying to use an object variable without a value.

Remember that the bit of the hold variable is a primitive and non-primitive do variable, but the difference is that the primitive variable holds the value in bits, but the non-primitive variable holds bits (for example, direction) to access the object in memory, and when it is no longer used, GGC (Garbage Colector) kills him.

0
source

Source: https://habr.com/ru/post/971814/


All Articles