Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources

异常信息:

java.lang.RuntimeException: Unable to start activity ComponentInfo{***.MainActivity}:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

启动Activity时,context为空。(Activity的context设置有问题)


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.empirecraft.war/com.empirewar.domination.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2443)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2503)
	at android.app.ActivityThread.-wrap11(ActivityThread.java)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:148)
	at android.app.ActivityThread.main(ActivityThread.java:5529)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
	at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
	at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
	at android.view.Window.getDefaultFeatures(Window.java:1349)
	at android.view.Window.(Window.java:480)
	at com.android.internal.policy.PhoneWindow.(PhoneWindow.java:305)
	at android.app.Activity.attach(Activity.java:6219)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
	... 9 more
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
	at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
	at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
	at android.view.Window.getDefaultFeatures(Window.java:1349)
	at android.view.Window.(Window.java:480)
	at com.android.internal.policy.PhoneWindow.(PhoneWindow.java:305)
	at android.app.Activity.attach(Activity.java:6219)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2503)
	at android.app.ActivityThread.-wrap11(ActivityThread.java)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:148)
	at android.app.ActivityThread.main(ActivityThread.java:5529)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

修复方法:

在Activity中添加super方法调用,设置Activity的context。

Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources_第1张图片

你可能感兴趣的:(android,java)