关于android.content.ContextWrapper.getResources(ContextWrapper.java:81)的一个空指针异常

本文介绍了在Android应用开发中,如何解决因过早调用getResource()方法导致的空指针异常问题。通过调整MyApplication类中实例化的时机,确保在onCreate()方法内完成初始化,从而避免了空指针异常。

今天在开发过程中,因为要对listview中显示的图片进行处理,类似现在的糗事百科那种图片显示效果,具体解决方案等以后再说,在下面的代码处抛出了这个异常:

DisplayMetrics dm = new DisplayMetrics();
		dm = MyApplication.getInstance().getResources().getDisplayMetrics();
经过断点调试发现,在MyApplication.getInstance()是可以获取到application的,但是在下面的getResource()这行代码中出现了空的错误,查看源码发现在变量的声明时,context还没有被传递进来,必须等到onCreate之后,才能使用context去获取资源,下面是开始报错时的写法:

public class MyApplication extends Application {
	private static MyApplication instance;
	public static DisplayImageOptions options; // DisplayImageOptions是用于设置图片显示的类
	public static DisplayImageOptions options_head; // DisplayImageOptions是用于设置图片显示的类
	private List<Activity> mList = new LinkedList<Activity>();
	private HashMap<String, Activity> map = new HashMap<String, Activity>();

	public static MyApplication getInstance() {
		if (instance == null) {
			instance = new MyApplication();
		}
		return instance;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		initImageLoader(this);
}
下面是改进之后的写法:

public class MyApplication extends Application {
	private static MyApplication instance = null;
	public static DisplayImageOptions options; // DisplayImageOptions是用于设置图片显示的类
	public static DisplayImageOptions options_head; // DisplayImageOptions是用于设置图片显示的类
	private List<Activity> mList = new LinkedList<Activity>();
	private HashMap<String, Activity> map = new HashMap<String, Activity>();

	public static MyApplication getInstance(){
		return instance;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		initImageLoader(this);
}
更改之后,解决了这个空指针异常。
``` FATAL EXCEPTION: main Process: com.oem.qisda, PID: 9507 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.oem.qisda/com.oem.qisda.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3690) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3923) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2444) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:205) at android.os.Looper.loop(Looper.java:294) at android.app.ActivityThread.main(ActivityThread.java:8223) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:685) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:977) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.content.ContextWrapper.getPackageName(ContextWrapper.java:177) at com.oem.qisda.MainActivity.<init>(MainActivity.java:57) at java.lang.Class.newInstance(Native Method) at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95) at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:44) at android.app.Instrumentation.newActivity(Instrumentation.java:1379) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3677) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3923)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2444)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loopOnce(Looper.java:205)  at android.os.Looper.loop(Looper.java:294)  at android.app.ActivityThread.main(ActivityThread.java:8223)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:685)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:977)```解决?
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值