无法在弹出窗口中存在的编辑文本上设置 Onclicklistener

Not able to set Onclicklistener on a editext present in a popup

我在 Activity 中实现了一个弹出窗口,我正在尝试向其添加 onclicklistener,但它显示错误 "Trying to invoke virtual method on a null object reference",但我检查了 edittext 的 id,它"date_from" 在 popupdashboard.xml 中,我是否遗漏了什么,我需要在某处添加 popdashboard.xml 的参考吗??

    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);

任何帮助将不胜感激

如果您的 编辑文本位于弹出窗口 window 内,那么您应该使用弹出上下文获取其视图。您可以执行以下操作: 请注意,在使用 active 查找其他视图之前,您首先要使用 activity 上下文获取其视图。

// 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);

下面的代码包含显示弹出窗口的设置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);