我想在啓動應用程序時顯示自定義對話框,並且不觸發任何事件,例如按鈕點擊。我把自定義對話框放在onCreate()
,但應用程序的NullPointerException
在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();
}
});
}
}
你試過到目前爲止是什麼? –