-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