我有一个包含 4 个按钮的应用程序,2 个在 MainActivity 中,2 个在自定义对话框中。当我在自定义对话框中为 2 个按钮调用 setOnClickListener() 时,它导致不幸的是 (app_name) 已在虚拟机中停止
public class MainActivity extends Activity implements OnClickListener{
Button btnAdd;
Button btnSet;
Button btnDlt;
Button btnCancel;
Dialog getDialog = new Dialog(this);
ListView taskList;
EditText getTask;
ArrayList<String> listItem = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpVeiw();
setUpListView();
setUpOnClick();
}
private void setUpVeiw() {
btnAdd = (Button) findViewById(R.id.btnAddTask);
btnDlt = (Button) findViewById(R.id.btnDlt);
btnSet = (Button) findViewById(R.id.btnSet);
btnCancel = (Button) findViewById(R.id.btnCancel);
getTask = (EditText) findViewById(R.id.editText1);
}
private void setUpListView() {
taskList = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listItem);
taskList.setAdapter(adapter);
listItem.add("Ali");
}
private void setUpOnClick() {
btnAdd.setOnClickListener(this);
btnDlt.setOnClickListener(this);
btnSet.setOnClickListener(this);
btnCancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAddTask:
getDialog.setContentView(R.layout.get_task_dialog);
getDialog.setTitle(R.string.add_task);
getDialog.show();
break;
case R.id.btnDlt:
break;
case R.id.btnSet:
break;
case R.id.btnCancel:
break;
default:
break;
}
}
}