Android 当检查null时,怎么会有NullPointerException?
注意:我知道这个问题是重复的,你可能会被鼓励投反对票,但是Android 当检查null时,怎么会有NullPointerException?,android,nullpointerexception,Android,Nullpointerexception,注意:我知道这个问题是重复的,你可能会被鼓励投反对票,但是 它发生在Android框架代码中 即使存在空检查且没有多线程,也会发生这种情况 我最近在playstore上部署了我的APK,并为少数用户收到了NullPointerException(4): 此异常源于行while((v=(View)listItem.getParent())!=null&&!v.equals(This)){ 在AdapterView.java中: /** * Returns the position
- 它发生在Android框架代码中
- 即使存在空检查且没有多线程,也会发生这种情况
我最近在playstore上部署了我的APK,并为少数用户收到了
NullPointerException
(4):
此异常源于行while((v=(View)listItem.getParent())!=null&&!v.equals(This)){
在AdapterView.java
中:
/**
* Returns the position within the adapter's data set for the view, where
* view is a an adapter item or a descendant of an adapter item.
* <p>
* <strong>Note:</strong> The result of this method only reflects the
* position of the data bound to <var>view</var> during the most recent
* layout pass. If the adapter's data set has changed without a subsequent
* layout pass, the position returned by this method may not match the
* current position of the data within the adapter.
*
* @param view an adapter item, or a descendant of an adapter item. This
* must be visible in this AdapterView at the time of the call.
* @return the position within the adapter's data set of the view, or
* {@link #INVALID_POSITION} if the view does not correspond to a
* list item (or it is not currently visible)
*/
public int getPositionForView(View view) {
View listItem = view;
try {
View v;
while ((v = (View) listItem.getParent()) != null && !v.equals(this)) {
listItem = v;
}
} catch (ClassCastException e) {
// We made it up to the window without find this list view
return INVALID_POSITION;
}
if (listItem != null) {
// Search the children for the list item
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
if (getChildAt(i).equals(listItem)) {
return mFirstPosition + i;
}
}
}
// Child not found!
return INVALID_POSITION;
}
这是在
AdapterView.java
中最终调用方法的代码。现在,当while循环中有空检查且所有UI处理仅在主线程中发生时,为什么会出现NullPointerException
?如果参数视图为空,则listItem也将为空。这将导致NPE:
listItem.getParent() != null
我想告诉你们一些关于空指针异常的提示
(v = (View) listItem.getParent()) != null && !v.equals(this)
如果listItem为null,它将创建异常…如果listItem不为null,但listItem.getParent()为null,它也将创建异常…或者当您将任何对象强制转换为另一个为null的对象时…它将引发异常
(v = (View) listItem.getParent()) != null && !v.equals(this)
在您的示例中,listItem.getParent()为null,这就是它引发异常的原因
(v = (View) listItem.getParent()) != null && !v.equals(this)
你应该这样做:
if(v != null && listItem != null && listItem.getParent() != null && (v = (View) listItem.getParent()) != null && !v.equals(this) {
}
我希望它不会抛出异常。我不得不覆盖该方法,我今天刚刚找到这个问题,但我有第665行,没有看到答案
(v = (View) listItem.getParent()) != null && !v.equals(this)
我最好的猜测是,修复不在某些API中,而是通过从API 27导入方法并稍微修改它以传入适配器
public int getPositionForView(View view, AdapterView av)
{
View listItem = view;
try
{
View v;
while ((v = (View) listItem.getParent()) != null && !v.equals(av))
{
listItem = v;
}
}
catch (ClassCastException e)
{
// We made it up to the window without find this list view
return -1;
}
if (listItem != null)
{
// Search the children for the list item
final int childCount = av.getChildCount();
for (int i = 0; i < childCount; i++)
{
if (av.getChildAt(i).equals(listItem))
{
return av.getFirstVisiblePosition() + i;
}
}
}
// Child not found!
return -1;
}
public int getPositionForView(视图视图,AdapterView av)
{
查看列表项=查看;
尝试
{
观点五;
而((v=(视图)listItem.getParent())!=null&&!v.equals(av))
{
listItem=v;
}
}
catch(ClassCastException e)
{
//我们在没有找到这个列表视图的情况下到达了窗口
返回-1;
}
如果(listItem!=null)
{
//在子项中搜索列表项
final int childCount=av.getChildCount();
for(int i=0;i
它总是有效的@YvetteColomb:NullPointerException非常通用,可以作为副本关闭。我希望对框架代码有所了解,因为毕竟存在空检查。您如何确定异常行?@Bob嗯,Play store的仪表板说异常发生在API级别22.S的第607行o、 我浏览了API级别22的源代码,发现这行代码中有equals(),这是异常产生的地方方法。这意味着,&&的第一个子句执行成功。但条件是,v!=null。如果为真,则它将自动中断!!是..它将中断..但如果在v.中膨胀视图,则它不会中断