-1

I have a popup implemented inside an Activity,and i'm trying to add onclicklistener to it,but it shows an error "Trying to invoke virtual method on a null object reference",but i checked the id of the edittext,which is "date_from" in popupdashboard.xml,am i missing something,do i need to add a reference for popdashboard.xml somewhere ??

    public class DashboardActivity2 extends AppCompatActivity implements View.OnClickListener {
ImageView image;
    EditText datefrom,dateto;
    TextView amo_today,amo_yest,increase,percent;


    private PopupWindow active;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard2);
        datefrom = (EditText)findViewById(R.id.date_from);
        datefrom.setOnClickListener(this);
      dateto = (EditText)findViewById(R.id.date_to);
       dateto.setOnClickListener(this);

any help would be appreciated

Suraj Makhija
  • 1,376
  • 8
  • 16
Venky
  • 1,929
  • 3
  • 21
  • 36
  • can you add your popcode in your activity – Sunisha Guptan May 11 '17 at 05:25
  • Could be because `date_from` is not in `activity_dashboard2` layout. Check this http://stackoverflow.com/questions/28193552/null-pointer-exception-on-setonclicklistener – jayeshsolanki93 May 11 '17 at 05:26
  • inflate the popupdashboard.xml to view and from which you get reference of edittext – Nidhin May 11 '17 at 05:28
  • @Nidhin it is being populated all right,but not right away,i'm looking for other eventlisteners,so i'm using it to populate when that event is called – Venky May 11 '17 at 05:48
  • @jayeshsolanki93 that is the first example i saw,i couldn't figure out anything from it,because i'm implementing Onclick method through my dashboardactivity,so i'm inflating popup and everyhting else in OnClick method which was autogenerated – Venky May 11 '17 at 05:52

2 Answers2

1

If your edit text is inside a popup window, then you should get its view using popup context. You can do the same as follow : Please note that, before you use active to find other view, you first to get its view using activity context.

// active = (PopupWindow)findViewById(R.id.active) // active is id of your popup in xml
datefrom = (EditText)active.findViewById(R.id.date_from);
dateto = (EditText)active.findViewById(R.id.date_to);
Aashish Aadarsh
  • 491
  • 5
  • 7
  • Should i define this in the Oncreate method ?? – Venky May 11 '17 at 05:46
  • i type casted layoutinflater,view and viewgroup outside oncreate and used the view inside a click event,and everything seems fine now,thanks a lot for your help – Venky May 11 '17 at 06:22
1

Below code contain setup for showing popup window

  active = new PopupWindow(ctx);
  inflater = (LayoutInflater) ctx
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layout = R.layout.popupdashboard;
  contentView = inflater.inflate(layout, null);
        datefrom = (EditText) contentView.findViewById(R.id.date_from);
  active.setHeight(LayoutParams.WRAP_CONTENT);
        active.setWidth(LayoutParams.WRAP_CONTENT);
  active.setContentView(contentView);
  active.showAtLocation(anchor, Gravity.NO_GRAVITY, position_x,
                position_y);
Nidhin
  • 1,818
  • 22
  • 23
  • Your answer gave me an insight,i type casted layoutinflater,view and viewgroup and used the view inside a click event,and everything seems fine now,thanks a lot for your help – Venky May 11 '17 at 06:21