1

I'm using the LayoutInflater within a Dialog and don't know what to set as a 2nd parameter, which is null for now.

I found answers for onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle), but that method isn't available for a Dialog.

Faking the null by something like (ViewGroup) null is not an option for me.

MyDialog

public class MyDialog extends Dialog implements View.OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view               = inflater.inflate(R.layout.my_dialog, null); 
        // ________________ How to replace that null? ___________________^

        setContentView(view);
    }
}

Error reported by Infer:

MyDialog.java:42: error: ERADICATE_PARAMETER_NOT_NULLABLE
  `LayoutInflater.inflate(...)` needs a non-null value in parameter 2 but argument `null` can be null. (Origin: null constant at line 42).
  41.           LayoutInflater inflater = LayoutInflater.from(getContext());
  42. >         View view               = inflater.inflate(R.layout.dialog_unlock, null);

Any ideas? Thanks in advance!

Solution

public class MyDialog extends Dialog implements View.OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog);

        Button myBtn         = findViewById(R.id.my_btn);
        EditText myTextField = findViewById(R.id.my_et);

        View.OnClickListener onClickMyBtn = v -> {
            String value = myTextField.getText().toString();
            Log.d("MyDialog", String.format("My value: %s", value));
            dismiss();
        };
        myBtn.setOnClickListener(onClickMyBtn);
    }
}
Mr. B.
  • 8,041
  • 14
  • 67
  • 117

1 Answers1

4

Use this

setContentView(R.layout.my_dialog);

Instead of this

LayoutInflater inflater = LayoutInflater.from(getContext());
View view  = inflater.inflate(R.layout.my_dialog, null);
setContentView(view);

SAMPLE CODE

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MyDialog extends Dialog implements View.OnClickListener {


    public MyDialog(Context context) {
        super(context);
    }

    Button myBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog);

        myBtn = findViewById(R.id.my_btn);
        myBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        if (view == myBtn) {
            Toast.makeText(view.getContext(), "clicked", Toast.LENGTH_SHORT).show();
        }
    }
}

Then create your dialog like this

MyDialog myDialog = new MyDialog(this);
myDialog.show();
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Thanks Nilesh, but how can I get the `View view` then? I need it to control a `Button` within the `Dialog`. – Mr. B. Jan 30 '19 at 10:06
  • @Mr.B. you can directly `findViewById` do inside `onCreate()` method like this `Button yes = findViewById(R.id.btn_yes);` than you can perform action as per your requirement – AskNilesh Jan 30 '19 at 10:09
  • @NileshRathod the problem is that, both methods are correct. His problem may be in context, `getContext()` may return null. Just pass context from your activity in a constructor, and your code will work. And sometimes code wont work, because there are needed all 3 arguments in `inflate()` method, here's only two. – grabarz121 Jan 30 '19 at 10:20
  • @NileshRathod I didn't know that, thanks. Unfortunately I'm getting a `NullPointerException` when clicking on the button, that I attached a `View.OnClickListener` to: `myBtn.setOnClickListener(onClickMyBtn)`. Any idea? – Mr. B. Jan 30 '19 at 10:23
  • 1
    @Mr.B. try updated answer first do `setContentView()` then do `findViewById()` – AskNilesh Jan 30 '19 at 10:42
  • 1
    @Mr.B.You cannot initialize button before you'll set your view. So your code will be work, when you move all code below `setContentView()`, this should be always after `super` method. – grabarz121 Jan 30 '19 at 10:45