We Rub Chan
We Rub Chan

Reputation: 205

How to show dialog immediately without any click?

I want to show the custom dialog when I start the application and without any event to trigger, e.g. button click. I put the custom dialog in onCreate(), but the application crahes for NullPointerException at btnDecline.setOnClickListener.

MainActivity.java

public class MainActivity extends Activity {

    private Button btnRegister, btnDecline, btnAccept;
    private EditText etRegisterName, etRegisterEmail;
    private Dialog terms;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        btnRegister = (Button)findViewById(R.id.btnRegister);
        etRegisterName = (EditText)findViewById(R.id.etRegisterName);
        etRegisterEmail = (EditText)findViewById(R.id.etRegisterEmail);

        btnRegister.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                String name = etRegisterName.getText().toString();
                String email = etRegisterEmail.getText().toString();
            }
        });

        showTermsConditions();
    }

    private void showTermsConditions(){
        terms = new Dialog(MainActivity.this);
        terms.setContentView(R.layout.terms_conditions_dialog);
        btnDecline = (Button) findViewById(R.id.btnDecline);
        btnAccept = (Button) findViewById(R.id.btnAccept);
        terms.setTitle("Terms and Conditions");
        terms.show();

        btnDecline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                moveTaskToBack(true);
            }
        });

        btnAccept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                terms.dismiss();
            }
        });
    }

}

Upvotes: 1

Views: 168

Answers (2)

deadfish
deadfish

Reputation: 12304

You are looking buttons id in wrong place

private void showTermsConditions(){
        terms = new Dialog(MainActivity.this);
        terms.setContentView(R.layout.terms_conditions_dialog);
        btnDecline = (Button) terms.findViewById(R.id.btnDecline);
        btnAccept = (Button) terms.findViewById(R.id.btnAccept);
        terms.setTitle("Terms and Conditions");
        terms.show();

        btnDecline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                moveTaskToBack(true);
            }
        });

        btnAccept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                terms.dismiss();
            }
        });
    }

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

Change to

terms.setContentView(R.layout.terms_conditions_dialog);
btnDecline = (Button) terms.findViewById(R.id.btnDecline);
btnAccept = (Button) terms.findViewById(R.id.btnAccept);

The buttons belong to the Dialog. You need to find views in that view hierarchy.

findViewById looks for a view in the current view hierarchy. The Buttons btnDecline and btnAccept do not belong to register.xml. They belong to terms_conditions_dialog.xml which is the layout for the Dialog.

Upvotes: 2

Related Questions