android – ContextWrapper.getResources()中的NullPointerException

前端之家收集整理的这篇文章主要介绍了android – ContextWrapper.getResources()中的NullPointerException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我真的不知道我正在构建的 Android应用程序发生了什么.在它开发的某个时刻,我开始在模拟器中启动它,并显示消息“Foo stopped”.这是错误日志,如果有人可以帮助我:
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)

解决方法

您正在从字段初始值设定项中调用getResources().就像是
private String mStr = getResources().getString(...);

(或使用drawables或其他类型的资源).

这是无效的,因为尚未设置Context.在onCreate()方法中移动此赋值.

猜你在找的Android相关文章