NullPointerException when inflating a PopupWindow - android

I'm trying to inflate a PopupWindow from a button that sits within list items (from a custom list adapter) but am getting NullPointerExceptions in multiple places.
Here's the onClick for the button where I'm trying to make the PopupWindow happen:
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = null;
if (scores[0] == null) {
pw = new PopupWindow(inflater.inflate(R.layout.tag_table_row, null, false), 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText("error!");
} else {
int x = tags.length;
int y = 5;
int z = Math.min(x, y);
for (int i = 0; i < z; i++) {
pw = new PopupWindow(inflater.inflate(R.layout.tag_table_row, null, false), 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText(tags[i]);
int tag1score = Double.parseDouble(scores[i]);
TextView score = (TextView) v.findViewById(R.id.tagscore);
score.setText(Integer.toString(tag1score));
}
}
pw.showAtLocation(v.findViewById(R.id.fragment_content), Gravity.CENTER, 0, 0);
}
I'm getting the NullPointerException at tag.setText(tags[i]);. If I comment that and just try to do scores, I get a NullPointerException at score.setText(Integer.toString(tag1score));. Both tags[] and scores[] are initialized and filled before the onClick, and I've tested to ensure that the results in both are Strings and NOT null.
Any thoughts as to why this NPE is happening?

You need to use the right thing wherein to look for the view. You are inflating into pw, but you are looking in v for your views. I think this might work better;
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = null;
PopupWindow pw = null;
if (scores[0] == null) {
popupView = layoutInflater.inflate(R.layout.tag_table_row, null, false);
pw = new PopupWindow(popupView, 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText("error!");
} else {
int x = tags.length;
int y = 5;
int z = Math.min(x, y);
for (int i = 0; i < z; i++) {
popupView = layoutInflater.inflate(R.layout.tag_table_row, null, false);
pw = new PopupWindow(popupView, 100, 100, true);
TextView tag = (TextView) popupView.findViewById(R.id.tag);
tag.setText(tags[i]);
int tag1score = Double.parseDouble(scores[i]);
TextView score = (TextView) popupView.findViewById(R.id.tagscore);
score.setText(Integer.toString(tag1score));
}
}
pw.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
it keeps track of the view you have inflated into, so you can search it for your elements.

Related

Aligning popup window near the button clicked

I have a custom popup window by a layout. I have to give a x,y coordinates to appear popup window after a_btn click. This can be different locations in different phones.
But I want to show the popup window always above and touching the the a_btn
How can I implement this.Help me
My code for showing the popup window :
a_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_view = lInflater.inflate(R.layout.popup_a, null);
final PopupWindow popup = new PopupWindow(popup_view,FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable());
popup.showAtLocation(relative, Gravity.NO_GRAVITY, coordinateTop, 100);
//popup.showAsDropDown(location_popup_view, 2, 2);
}
});
Use this code will help you
a_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Rect r = locateView(v);
LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_view = lInflater.inflate(R.layout.popup_a, null);
final PopupWindow popup = new PopupWindow(popup_view,FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable());
popup.showAtLocation(layout, Gravity.TOP | Gravity.LEFT, r.right, r.bottom);
}
});
public static Rect locateView(View v) {
int[] loc_int = new int[2];
if (v == null)
return null;
try {
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe) {
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = loc_int[0] + v.getWidth();
location.bottom = loc_int[1] + v.getHeight();
return location;
}
I used popup.showAsDropDown(a_btn,0,0);
instead of
popup.showAtLocation(relative, Gravity.NO_GRAVITY, coordinateTop, 100);
and gave xoff and yoff.
a_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_view = lInflater.inflate(R.layout.popup_a, null);
final PopupWindow popup = new PopupWindow(popup_view,200,200,true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable());
popup.showAsDropDown(a_btn,0,0);
}
});

How to center PopupWindow?

I created an Activity in which i add a button that throws a popup when is clicked. Here is the code of showPopup() method:
private void showPopup() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);
final PopupWindow pwindo = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);
Button btnAgree = (Button) layout.findViewById(R.id.ok);
btnAgree.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
}
I would center it both vertically and orizzontally. I tried several ways that i see here on SO but none worked. Why i always get the popup window at the top of the screen?
you can use setHorizontalOffset:
ListPopupWindow popupWindow = new ListPopupWindow(this);
// Position
popupWindow.setAnchorView(btn);
popupWindow.setPromptPosition (ListPopupWindow.POSITION_PROMPT_ABOVE);
popupWindow.setHorizontalOffset((btn.layoutParams.width - popupWindow.getWidth())/2);
this code will show perfect popup in center of you specific view
// click event of specific view
#Override
public void onClick(View v) {
super.onClick(v);
if (v == lin_filterActionbar) {
PopupWindow popupwindow_obj; // create object
popupwindow_obj = dialog_AllergiesSelect(RR_HomeActivity.this,lin_filterActionbar);
popupwindow_obj.showAsDropDown(lin_filterActionbar);
}
}
private PopupWindow dialog_AllergiesSelect(Context context,LinearLayout lin) {
final PopupWindow dialog_AllergiesSelect = new PopupWindow(this);
View v = View.inflate(context, R.layout.dialog_filter_actionbar, null);
dialog_AllergiesSelect.setContentView(v);
dialog_AllergiesSelect.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setFocusable(true);
// TextView lis_testSelection = (TextView) v.findViewById(R.id.dialogfilter_txt_filter);
// LinearLayout.LayoutParams lin_laLayoutParams = new LinearLayout.LayoutParams((int) (sunApplication.getDeviceWidth() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_spinner_width)), (int) (sunApplication.getDeviceHeight() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_height)));
// lis_testSelection.setLayoutParams(lin_laLayoutParams);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
dialog_AllergiesSelect.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
//dialog_AllergiesSelect.showAtLocation(v, Gravity.C, p.x + OFFSET_X, p.y + OFFSET_Y);
Rect location = locateView(lin);
dialog_AllergiesSelect.showAtLocation(v, Gravity.TOP|Gravity.CENTER, 0, location.bottom);
// Getting a reference to Close button, and close the popup when clicked.
return dialog_AllergiesSelect;
}
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}

Popup showAtLocation is not working

I'm using pop-window to show some text-view while clicking the edit-text . but pop-window is not showing at particular location ,it always showing at left top corner , here is my code ,what is wrong in that
private void showPopup(Context context,final LinearLayout Parent) {
LayoutInflater layoutInflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.popup,MainActivity.parent,true);
// Creating the PopupWindow
final PopupWindow popupWindow = new PopupWindow(
layout,700,700);
popupWindow.setContentView(layout);
popupWindow.setHeight(500);
new Runnable(){
#Override
public void run() {
popupWindow.showAtLocation(layout, Gravity.CENTER,300,150);
}
};
}
Try Following Code:
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException e)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}
And then use
popup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, location.left, location.bottom);
Hope it helps...

Get width layout when layout add view dynamically

I have a problem when I getWidth of layout... It doesn't return a real width after adding new view?... how can I solve it... thanks... This is my sample code.
llDayHeaderTable = (LinearLayout) viewGroup
.findViewById(R.id.llDayHeaderTable);
LayoutInflater vi = (LayoutInflater) parent
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (View v : vTmp) {
llDayHeaderTable.removeView(v);
}
vTmp.removeAllElements();
for (int i = 0; i < dto.arrProductTitleList.size(); i++) {
String productName = dto.arrProductTitleList.get(i).productInfoName;
vItemHeader = vi.inflate(R.layout.layout_kpi_sku_group_accum_item,
null);
TextView tv = (TextView) vItemHeader
.findViewById(R.id.tvHeaderProduct);
tv.setText("" + productName);
llDayHeaderTable.addView(vItemHeader);
vTmp.add(vItemHeader);
}
int width = llDayHeaderTable.getWidth();
Try using getMeasuredHeight() and getMeasuredWidth() instead of getWidth() and getHeight()
llDayHeaderTable.measure(0, 0);
final int w = llDayHeaderTable.getMeasuredWidth();
final int h = llDayHeaderTable.getMeasuredHeight();

Listview with onClick popup information

I'm creating an app which should list some snowboard tricks and when one clicks on a trick, a popup should appear with information on the trick and how to do it (with text and maybe a youtube vid).
How does one realise this?
Here is a code for Creating a custom list adapter with a popup window that pop up when you press a certain image in the ListView row:
Here is the Adapter:
public class tasksRepositoryAdapter extends ArrayAdapter<Task>
{
private ArrayList<Task> list;
public tasksRepositoryAdapter(Context context, int textViewResourceId, List<Task> tasksRepository)
{
super(context, textViewResourceId, tasksRepository);
this.list = new ArrayList<Task>();
for (Task task : tasksRepository)
{
this.list.add(task);
}
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row;
final ViewHolder holder = new ViewHolder();
tfRobotoRegular = Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf");
LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflator.inflate(R.layout.new_row, null);
holder.tvTitle = (TextView) row.findViewById(R.id.text_title);
String title = tasksRepository.get(position).getTitle();
if (title.length()>25)
{
title = title.substring(0, 24);
title = title + "...";
}
holder.tvTitle.setText(title);
holder.tvTitle.setTypeface(tfRobotoRegular);
holder.tvDate = (TextView) row.findViewById(R.id.text_date);
holder.tvDate.setText(tasksRepository.get(position).getDate());
holder.tvDate.setTypeface(tfRobotoRegular);
holder.tvTime = (TextView) row.findViewById(R.id.text_time);
holder.tvTime.setText(tasksRepository.get(position).getTime());
holder.tvTime.setTypeface(tfRobotoRegular);
holder.tvDescription = (TextView) row.findViewById(R.id.text_description);
String description = tasksRepository.get(position).getDescription();
if (description.length()>46)
{
description = description.substring(0, 45);
description = description + "...";
}
holder.tvDescription.setText(description);
holder.tvDescription.setTypeface(tfRobotoRegular);
holder.tvId = (TextView) row.findViewById(R.id.text_id);
holder.tvId.setText(String.valueOf(tasksRepository.get(position).getId()));
holder.tvId.setTypeface(tfRobotoRegular);
holder.tvLocation = (TextView) row.findViewById(R.id.text_location);
holder.tvLocation.setText(tasksRepository.get(position).getCity());
holder.llRowLayout = (LinearLayout) row.findViewById(R.id.llRowLayout);
holder.imCalendar = (ImageView) row.findViewById(R.id.iCalendar);
holder.imClock = (ImageView) row.findViewById(R.id.iClock);
holder.imLocation = (ImageView) row.findViewById(R.id.iLocation);
holder.imTaskStatusButton = (ImageView) row.findViewById(R.id.iTaskStatusButton);
holder.imTaskStatusButton.setTag(position);
holder.imTaskStatusButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
int[] location = new int[2];
currentRowId = position;
currentRow = v;
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
v.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
point = new Point();
point.x = location[0];
point.y = location[1];
showStatusPopup(TasksListActivity.this, point);
}
});
String status = tasksRepository.get(position).getStatus();
Log.d(TAG, "The status of the current row: "+ status );
setStatusColorImages(status, holder.imClock, holder.imCalendar, holder.imLocation, holder.llRowLayout);
return row;
}
}
Here is the ViewHolder:
static class ViewHolder
{
RelativeLayout rlTitle;
LinearLayout llRowLayout;
TextView tvId;
TextView tvTitle;
TextView tvDate;
TextView tvTime;
TextView tvDescription;
TextView tvLocation;
ImageView imClock;
ImageView imCalendar;
ImageView imLocation;
ImageView imTaskStatusButton;
}
And the PopUp Window:
// The method that displays the popup.
private void showStatusPopup(final Activity context, Point p) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llStatusChangePopup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.status_popup_layout, null);
// Creating the PopupWindow
changeStatusPopUp = new PopupWindow(context);
changeStatusPopUp.setContentView(layout);
changeStatusPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setFocusable(true);
// Some offset to align the popup a bit to the left, and a bit down, relative to button's position.
int OFFSET_X = -20;
int OFFSET_Y = 50;
//Clear the default translucent background
changeStatusPopUp.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
changeStatusPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}

Categories

Resources