-1

出现editext我有一个活动内实现一个活动,并试图添加onclicklistener它,但它显示一个错误“试图调用空对象上的虚拟方法引用“,但我检查了editd的id,它是popupdashboard.xml中的”date_from“,我是否缺少某些东西,是否需要为popdashboard.xml某处添加引用?无法设置Onclicklistener在弹出

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

可以添加你的Pop代码在你的活动 –

+0

可能是因为'date_from'不'activity_dashboard2'布局。检查这个http://stackoverflow.com/questions/28193552/null-pointer-exception-on-setonclicklistener – jayeshsolanki93

+0

膨胀popupdashboard.xml查看和从中你可以得到edittext的参考 – Nidhin

回答

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

你的回答给了我一个见解,我键入了casttedflater,view和viewgroup,并在点击事件中使用了视图,现在一切都很好,非常感谢你的帮助 – Venky

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

我应该在Oncreate方法中定义此? – Venky

+0

是的,onCreate活动的方法 –

+0

我键入castinflater,查看和viewgroup外部oncreate和使用视图内点击事件,现在一切似乎很好,非常感谢您的帮助 – Venky