在code查看Android的工作室空指针警告/通知 [英] Android studio nullpointer warnings/notice in code view

查看:233
本文介绍了在code查看Android的工作室空指针警告/通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到现在为止我无法找到一个适合我的问题/问题的任何答案。
在Android Studio中往往显示了一个通知,如findViewById可能会产生NPE,althogh我知道该项目在我用的是现有的布局。
我也从来没有得到一个在NPE在运行时的位置抛出,那么为什么警告/通知?

我不知道我怎么有反应/句柄在code查看这些警告/相关信息?是否有任何提示,以避免消息(因为对我来说,它看起来像合法和工作$ C $三)?这只是在code查看的警告,这让我不确定。

下面凡findViewByID会显示警告信息片段。

  //获取predefined项目布局
行= inflater.inflate(R.layout.view_searchresult_item,NULL);
//获取表布局插入项目
TableLayout itemtablelayout =(TableLayout)row.findViewById(R.id.resultitem_tablelayout);


解决方案

警告是不是一个错误。和你谈论警告说,它可能会产生,不要说它必须产生。因此,选择是你的。无论是加空检查或不


所以,如果你确信 findViewById 在code将永远 NPE的原因,然后不加空检查。

until now I couldn't find any answer that suits my problem/question. In android studio it often shows a notice that e.g. findViewById may produce a NPE, althogh i know the item IS existing in the layout I use. I also never got a NPE thrown at this locations at runtime, so why the warning/notice?

I just don't know how I have to react/handle to such warnings/infos in the code view? Are there any tips to avoid that messages (because for me it looks like valid and working code)? It's only the warnings in the code view, that makes me uncertain.

Here a snippet where a warning is shown on findViewByID.

//fetch predefined item layout
row = inflater.inflate(R.layout.view_searchresult_item,null);
//get table layout for inserting items
TableLayout itemtablelayout = (TableLayout)row.findViewById(R.id.resultitem_tablelayout);

解决方案

Warning is not an Error. And the warning which you are talking about says "it may produce", don't say 'it must produce'. So choice is yours. Either add null check or not


So, If you are sure that findViewById in your code will never be cause of NPE, then don't add the null check.

这篇关于在code查看Android的工作室空指针警告/通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆