-2

After adding the code from call.eneque I get the error from the title it says a Nullpointerexception on my clickListener, but everything is initialized or do I overlook something? I looked for hours now tried different tutorials and fixes but can not find anything.

Here is my code:

public void openDialog(){
        AlertDialog.Builder formBuilder = new AlertDialog.Builder(FriendListActivity.this);
        View formView = getLayoutInflater().inflate(R.layout.contact_add_form,null);
        final AlertDialog dialog = formBuilder.create();
        final EditText nameText = dialog.findViewById(R.id.nameInput);
        formBuilder.setView(formView);
         dialog.show();


        // Buttons

        sButton = dialog.findViewById(R.id.saveButton);
        sButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PalaverService friendsAddService = PalaverApiClient.getInstance();
                final String FriendName = nameText.getText().toString();
                Call<ApiResponse> call = friendsAddService.addFriend("", "", FriendName);
                call.enqueue(new Callback<ApiResponse>() {
                    @Override
                    public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
                        ApiResponse responseSuccess = response.body();

                        if(responseSuccess.getMessageType().equals(1)){
                            friendListAdapter.add(FriendName);
                        }
                        dialog.hide();

                    }

                    @Override
                    public void onFailure(Call<ApiResponse> call, Throwable t) {

                    }
                });



            }
        });

        abbrechen = dialog.findViewById(R.id.abbrechen);
        abbrechen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    dialog.hide();
            }
        });



    }

This is my error:

  Process: de.paluno.se.palaver, PID: 16209
        java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

The error is coming on my setOnclickListener. Hopefully someone can give me an pointer.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

1

you have to get your button widget from the formView

    View formView = getLayoutInflater().inflate(R.layout.contact_add_form,null);
    final EditText nameText = formView.findViewById(R.id.nameInput);
    sButton = formView.findViewById(R.id.saveButton);
ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
  • Hi, I did that the buttons now don't give me an error but now my dialog is not shown only the display is getting a bit darker but I still see the old View – user11343077 Jun 22 '19 at 18:57
  • Check this question it will help you because you are building your dialog wrongly ... and also please consider my answer if it helped you to fix your first problem – ismail alaoui Jun 22 '19 at 19:56
  • which question do you mean? Yes your question did help me with my first problem. What do you mean I am building my dialog wrong? – user11343077 Jun 22 '19 at 21:03
  • https://stackoverflow.com/questions/2115758/how-do-i-display-an-alert-dialog-on-android – ismail alaoui Jun 22 '19 at 21:09