Хороший день для всех программистов. У меня есть проблема NullPointerException, когда я вызываю метод showAtLocation of PopupWindow. Как и во многих форумах, это исключение происходит потому, что первый параметр метода showAtLocation имеет значение null. Поэтому я проверить это следующим образом:NullPointErexception при вызове showAtLocation
showAtLocation (View parent, int gravity, int x, int y)
parent.equals(null) //- it returns false
LinearLayout lout = (LinearLayout) parent;
lout.getChildCount() //- it returns true count of child elements
((TextView) lout.getChildAt(1)).getText() //- it returns a text which i write in android:text field
У меня есть Gridview и его адаптер является CustomAdapter (расширяет BaseAdapter). В этом классе (CustomAdapter) есть OnClickListener в getView метод. Я хочу установить popupwindow для каждого элемента этого gridview. Таким образом, в OnClickListener я называю метод showPopup:
private void showPopup(final Activity context, Point p) {
int popupWidth = 200;
int popupHeight = 150;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInflater = prnt.getLayoutInflater();
View parent = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(parent);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
int OFFSET_X = 30;
int OFFSET_Y = 30;
popup.setBackgroundDrawable(new BitmapDrawable());
/*
LinearLayout lout = (LinearLayout) parent;
showMsg(parent.equals(null) + " : type " + lout.getChildCount() + " - " + ((TextView) lout.getChildAt(1)).getText());
*/
popup.showAtLocation(parent, 0, p.x + OFFSET_X, p.y + OFFSET_Y); //error occurs here
}
Пожалуйста, мне нужна ваша помощь
Возможный дубликат [Что такое исключение нулевого указателя, и как это исправить?] (http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – soorapadman