Question

My app is showing a Dialog from my activity onCreate. So I have created a CustomDialog and it is working fine.

I want to get a value of EditText of Parent Activity in CustomDialog but it is giving me a NULLPointerExecption

Here is my code:

Dialog:

public class VerifyDialog extends Dialog implements
android.view.View.OnClickListener {


    public Activity c;
    public Dialog d;
    public Button yes;

    public VerifyDialog(Activity a) {
        super(a);
        // TODO Auto-generated constructor stub
        this.c = a;
      }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.verify_dialog);
        yes = (Button) findViewById(R.id.btn_yes);
        yes.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Check();




                //Sending new Activity to verify pin of user

                Intent i = new Intent(c, ProcessPinCode.class);
                EditText phone_number = (EditText) findViewById(R.id.phone_number);
                String phone = phone_number.getText().toString();
                i.putExtra("response", phone);
                c.startActivity(i);
            }

            private void Check() {
                // TODO Auto-generated method stub
                String OrgPin;


                VerifyActivity v = new VerifyActivity();
                String new_txt = v.t1.getText().toString();
                System.out.print(new_txt);//**here i want to show**
                OrgPin = v.getPinCode();


                EditText org = (EditText) findViewById(R.id.pin_code_user);

                if(!OrgPin.equals(org))
                {
                    Toast.makeText(getContext(), "Pin Code not match", Toast.LENGTH_LONG).show();
                }
            }

        });
    }

    @Override
      public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_yes:
          c.finish();
          break;
        default:
          break;
        }
        dismiss();
      }
}

Calling Dialog form activity named 'VerifyActivity':

VerifyDialog cdd=new VerifyDialog(VerifyActivity.this);
            cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            cdd.show();

Is there any way to get value of EditText of VerifyActivity Parent in Dialog??

Thanks for taking look

Was it helpful?

Solution

As you already have a pointer to your parent Activity, simply changing the code to this should work:

EditText phone_number = (EditText) c.findViewById(R.id.phone_number);

At the moment, you are trying to find a view in the dialog's layout with the id R.id.phone_number, but since that EditText is actually in the activity's layout it is not found in the dialog.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top