0

I have a function which creates a button, which has an onClick event to bring up a custom dialog which just shows some information from the functions parameters. The dialog has two buttons on it as well, one to close the dialog, and one to add information to a file.

When I try to set onClick events for those buttons, the app crashes, and the error I get is a NullPointerException which says that I am trying to invoke a virtual method on a null object reference.

If I comment out the part where I set the onClickEventListener code for both buttons, then the dialog appears as normal, with the buttons on it.

Note: context is a variable declared in the class. It is simply Context context = this

Code is below:

public void addButton(String text, int id, String areas, String details, String notes) {
    Button button = new Button(this);
    final String title = "Add "+text;
    final String dName = text;
    final String dAreas = areas;
    final String dDetails = details;
    final String dNotes = notes;
    button.setText(text);
    button.setTextColor(ContextCompat.getColor(context, R.color.buttonText));
    button.setTextSize(32);
    button.setId(id);
    if (isEven(id+1)) {
        button.setBackgroundResource(R.drawable.buttonshapeother);
    } else {
        button.setBackgroundResource(R.drawable.buttonshape);
    }
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Make custom dialog
            final Dialog dialog = new Dialog(context);
            Button add = (Button) dialog.findViewById(R.id.btnAddExer);
            Button cancel = (Button) dialog.findViewById(R.id.btnCancel);
            dialog.setContentView(R.layout.popup_exercise);
            dialog.setTitle(title);

            // Set the custom components now
            TextView tName = (TextView) dialog.findViewById(R.id.lblNameData);
            TextView tAreas = (TextView) dialog.findViewById(R.id.lblAreaData);
            TextView tDetails = (TextView) dialog.findViewById(R.id.lblDetailsData);
            TextView tNotes = (TextView) dialog.findViewById(R.id.lblNotesData);
            tName.setText(dName);
            tAreas.setText(dAreas);
            tDetails.setText(dDetails);
            tNotes.setText(dNotes);

            // Add functions to buttons
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (addExercise(dName, dAreas, dDetails, dNotes)) { // Add exercise to user's workout
                        Toast.makeText(context, "Exercise was added to your workout", Toast.LENGTH_LONG).show();
                        dialog.dismiss(); // Close dialog
                    } else {
                        Toast.makeText(context, "There was an error adding your exercise", Toast.LENGTH_LONG).show();
                    }
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss(); // Close dialog
                }
            });

            dialog.show(); // Actually show the dialog
        }
    });
    LinearLayout lay = (LinearLayout) findViewById(R.id.innerLay);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lay.addView(button, params);
}

public boolean isEven(int num) {
    if ((num&1) == 0) {
        return true;
    } else {
        return false;
    }
}
Jono
  • 199
  • 3
  • 15

1 Answers1

2

Because you are trying to find button view before setting the layout to it. So try like this:

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.popup_exercise);
dialog.setTitle(title);
Button add = (Button) dialog.findViewById(R.id.btnAddExer);
Button cancel = (Button) dialog.findViewById(R.id.btnCancel);
Rohit Arya
  • 6,751
  • 1
  • 26
  • 40