• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
风和月
博客园    首页    新随笔    联系   管理    订阅  订阅

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开启时就会发生闪退

 


       

posted @ 2020-04-18 14:59  风和月  阅读(450)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3