C-Gian
C-Gian

Reputation: 169

Android studio custom dialog button null object reference

I'm trying to do a modal view with a custom dialog, this is myDialog.java class:

public class MyDialog extends Dialog implements android.view.View.OnClickListener {

    public Activity c;
    public Dialog d;
    public Button btnaddsingleingredient, btncanceladdingsingleingredient;

    public MyDialog(Activity a) {
        super(a);
        // TODO Auto-generated constructor stub
        this.c = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.my_dialog);
        btnaddsingleingredient = (Button) findViewById(R.id.btnaddsingleingredient);
        btncanceladdingsingleingredient = (Button) findViewById(R.id.btncanceladdingsingleingredient);
        btnaddsingleingredient.setOnClickListener(this);
        btncanceladdingsingleingredient.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnaddsingleingredient:
                d.dismiss();
                break;
            case R.id.btncanceladdingsingleingredient:
                d.dismiss();
                break;
            default:
                break;
        }
        dismiss();
    }
}

This is the class where I call the dialog:

public class AggiungiIngredientiActivity extends AppCompatActivity {

    ImageButton btnAddNewIngredient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aggiungi_ingredienti);

        btnAddNewIngredient = findViewById(R.id.btnAddNewIngredient);

        btnAddNewIngredient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){

                MyDialog addIngredientDialog = new MyDialog(AggiungiIngredientiActivity.this);

                addIngredientDialog.btncanceladdingsingleingredient = findViewById(R.id.btncanceladdingsingleingredient);


                addIngredientDialog.btnaddsingleingredient.findViewById(R.id.btnaddsingleingredient).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });

Basically I want to open a modal view to let the user insert some data but it gives me the error:

Attempt to invoke virtual method 'android.view.View android.widget.Button.findViewById(int)' on a null object reference

and it says that the problem is here:

addIngredientDialog.btnaddsingleingredient.findViewById(R.id.btnaddsingleingredient).setOnClickListener(new View.OnClickListener())) { ... }

I don't know if this is the best way to do a modal view(i'm open to suggests), if you need the xml I can update the post but I think it's not relevant

Update 1: I tried also

btnaddsingleingredient = (Button) findViewById(R.id.btnaddsingleingredient); //Replace sat1 with id defined in XML layout

instead of

addIngredientDialog.btnaddsingleingredient.findViewById(R.id.btnaddsingleingredient).setOnClickListener

Upvotes: 0

Views: 462

Answers (1)

C-Gian
C-Gian

Reputation: 169

Solved this, I put here the solution in case someone need it too. Basically instead of creating a custom class that extends Dialog just use the class Dialog and then use dialog.setContentView(R.layout.yourlayout); Then you reference the button with dialog.findViewById(R.id.idbuttoninthecustomalert); And finally you can you setOnClickListener without the null reference!

Dialog mydialog;

Button button,

mydialog = new Dialog(context.this);

mydialog.setContentView(R.layout.yourlayout);

button= mydialog.findViewById(R.id.buttonid);

Upvotes: 1

Related Questions