我收到一个空指针异常,我想在理解原因方面提供一些帮助。我正在创建一个自定义下拉式抽屉。我有一个视图,出现在我按下按钮上的活动中(现在,直到我执行fling)。这就是我设置PopupWindow的方式
popupMessage = new PopupWindow(view, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, true);
popupMessage.setContentView(view);
// Clear the default translucent background
popupMessage.setBackgroundDrawable(null);
if(userType != null){
if (userType.contentEquals("kid")) {
popupMessage.setFocusable(false);
} else {
popupMessage.setFocusable(true);
}
}
popupMessage.setOutsideTouchable(true);
popupMessage.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
popupMessage.setAnimationStyle(R.style.PopupAnimation);
popupMessage.update();
// Icon is an imageVIew ** This is where the null pointer occurs **
popupMessage.showAsDropDown(icon, 0, -(35+8));
为什么会导致空指针异常?我也试过
popupMessage.showAtLocation(icon, Gravity.TOP|Gravity.LEFT, 0, 0);
这是我的日志
答案 0 :(得分:1)
popupMessage显然不是null,因为您可以在空指针之前对实例执行方法。因此我的猜测是图标为空,请确认是否是这种情况。
popupMessage.setOutsideTouchable(true);
popupMessage.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
popupMessage.setAnimationStyle(R.style.PopupAnimation);
popupMessage.update();
// Icon is an imageVIew ** This is where the null pointer occurs **
popupMessage.showAsDropDown(icon, 0, -(35+8));