當我創建一個對話框並在按鈕上設置onClickListener時,應用程序崩潰。相同的代碼在另一個活動中起作用,那麼情況如何?Android對話框Button.setonClickListener
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
initNewUserDialog();
initNewLocationDialog();
...
private void initNewLocationDialog() {
new_location_Dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
setContentView(R.layout.new_location);
new_location_Button = (Button)new_location_Dialog.findViewById(R.id.newlocation_ok);
//Crash here
new_location_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new_location_Dialog.dismiss();
}
});
new_location_editText = (EditText)new_location_Dialog.findViewById(R.id.newlocation_edittext);
new_location_Dialog.hide();
}
logcat的嘗試,請... –
錯誤是空指針異常? – Mou
爲什麼你有另一個'setContentView(R.layout.new_location);'? –