tenten tenten - 6 months ago 18
Android Question

Setting onClickListner for items within popup window in android

I have a popup window that contains 2 text views.

If user click on one text view toast must appear.I coded for this function but shows

Nullpointer exception
in line
far.setOnClickListener(new OnClickListener() {
Please help me on this.

My code :

btn_a.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,75,true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable());
popup.showAsDropDown(btn_a, 0,0);

TextView far = (TextView) rootView.findViewById(R.id.fartext);
far.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked Far", Toast.LENGTH_SHORT).show();

}
});
}
});

Answer

Change this line

TextView far = (TextView) popup_view.findViewById(R.id.fartext);

instead of this

TextView far = (TextView) rootView.findViewById(R.id.fartext);