我在Activity中实现了一个弹出窗口,并且我试图向它添加onclicklistener,但它显示错误"尝试在空对象引用上调用虚方法"但是我检查了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);
任何帮助将不胜感激
答案 0 :(得分:1)
如果编辑文本位于弹出窗口中,则应使用弹出上下文获取其视图。你可以这样做: 请注意,在使用有效查找其他视图之前,您首先要使用活动上下文获取其视图。
// 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);
答案 1 :(得分: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);