【问题标题】:Not able to set Onclicklistener on a editext present in a popup无法在弹出窗口中的编辑文本上设置 Onclicklistener
【发布时间】:2017-10-09 23:13:32
【问题描述】:

我在 Activity 中实现了一个弹出窗口,我正在尝试向其中添加 onclicklistener,但它显示错误“尝试在空对象引用上调用虚拟方法”,但我检查了 edittext 的 id,这是popupdashboard.xml中的“date_from”,我错过了什么,我需要在某处添加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);

任何帮助将不胜感激

【问题讨论】:

  • 你能在你的活动中添加你的popcode吗
  • 可能是因为date_from 不在activity_dashboard2 布局中。检查这个stackoverflow.com/questions/28193552/…
  • 膨胀 popupdashboard.xml 以查看并从中获取edittext的参考
  • @Nidhin 它正在被填充,但不是马上,我正在寻找其他事件监听器,所以我在调用该事件时使用它来填充
  • @jayeshsolanki93 这是我看到的第一个例子,我无法从中找出任何东西,因为我正在通过我的仪表板活动实现 Onclick 方法,所以我在 OnClick 中膨胀弹出窗口和其他所有内容自动生成的方法

标签: android android-edittext popup onclicklistener android-popupwindow


【解决方案1】:

以下代码包含显示弹出窗口的设置

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

【讨论】:

  • 你的回答给了我一个洞察力,我输入了 casted layoutinflater、view 和 viewgroup 并在点击事件中使用了视图,现在一切都很好,非常感谢你的帮助
【解决方案2】:

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

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

【讨论】:

  • 我应该在 Oncreate 方法中定义这个吗??
  • 我在 oncreate 之外键入 casted layoutinflater、view 和 viewgroup 并在点击事件中使用了视图,现在一切正常,非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多