Answer the question
In order to leave comments, you need to log in
Why does a NullPointerException appear when clicking on a button in a dialog box?
In the application, I use a FloatingActionButton and after clicking it, a dialog box opens where I will enter data. The click handler for the FloatingActionButton works fine, but the handler for another button in the dialog throws a NullPointerException. The dialog box itself opens normally with input fields, but even before the button is clicked, the catch block is processed in the dialog box. Why am I getting this exception? For clarity, here is the code for the onCreate method.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subjectsList = new ArrayList<Subjects>();
listView = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<Subjects>(this, android.R.layout.simple_list_item_1, subjectsList);
listView.setAdapter(adapter);
addSubjectButton = (FloatingActionButton) findViewById(R.id.addSubjectButton);
addSubjectDialogButton = (Button) findViewById(R.id.addSubjectDialogButton);
// Создание диалога для ввода данных про предмет
dialog = new Dialog(MainActivity.this);
dialog.setTitle("Додавання предмету");
dialog.setContentView(R.layout.subject_dialog);
TextView enterSubjectInfo = (TextView) dialog.findViewById(R.id.enterSubjectInfoView);
enterSubjectInfo.setText("Назва предмету");
final EditText enterSubject = (EditText) dialog.findViewById(R.id.enterSubjectView);
TextView enterCoefficientInfo = (TextView) dialog.findViewById(R.id.enterCoefficientInfoView);
enterCoefficientInfo.setText("Коефіцієнт");
final EditText enterCoefficient = (EditText) dialog.findViewById(R.id.enterCoefficientView);
TextView enterGradeInfo = (TextView) dialog.findViewById(R.id.enterGradeInfoView);
enterGradeInfo.setText("Оцінка");
final EditText enterGrade = (EditText) dialog.findViewById(R.id.enterGradeView);
addSubjectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show(); // Выводим диалоговое окно на экран
// NULL POINTER EXCEPTION!!!!!!
try {
addSubjectDialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Subjects sub = new Subjects(enterSubject.getText().toString(), Double.parseDouble(enterCoefficient.getText().toString()), Double.parseDouble(enterGrade.getText().toString()));
//sub.toString();
//adapter.add(sub);
enterSubject.setText("good");
}
});
} catch (NullPointerException ex) {
enterSubject.setText("bad");
}
}
});
}
Answer the question
In order to leave comments, you need to log in
Come on, what do you expect from this code?
addSubjectDialogButton = (Button) findViewById(R.id.addSubjectDialogButton);
addSubjectDialogButton
zero, which means that at the time of searching for a button with such an id, it simply was not in the activation markup. It is understandable, this button is in the markup of the dialog. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question