Attempt to invoke virtual method 'void com.loopj.android.image.SmartImageView.setImageUrl(java.lang.String, java.lang.Integer, java.lang.Integer)' on a null object reference

使用ListView的适配器时报错:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.loopj.android.image.SmartImageView.setImageUrl(java.lang.String, java.lang.Integer, java.lang.Integer)' on a null object reference

出错代码:

1 View view1 = View.inflate(MainActivity.this, R.layout.news_item, null);
2 SmartImageView siv_icon = findViewById(R.id.siv_icon);
3 TextView tv_title = findViewById(R.id.tv_title);
4 TextView tv_description = findViewById(R.id.tv_description);
5 TextView tv_type = findViewById(R.id.tv_type);

正确代码:

1 View view1 = View.inflate(MainActivity.this, R.layout.news_item, null);
2 SmartImageView siv_icon = view1.findViewById(R.id.siv_icon);
3 TextView tv_title = view1.findViewById(R.id.tv_title);
4 TextView tv_description = view1.findViewById(R.id.tv_description);
5 TextView tv_type = view1.findViewById(R.id.tv_type);
6     

虽然第一种代码在编译器里并不会报错,但是在app开启时就会发生闪退


       

猜你喜欢

转载自www.cnblogs.com/y-yang/p/12725934.html