【问题标题】:Android (Java): how to use LayoutInflater.inflate() for Dialogs?Android (Java):如何将 LayoutInflater.inflate() 用于对话框?
【发布时间】:2019-06-23 13:24:55
【问题描述】:

我在Dialog 中使用LayoutInflater,但不知道将什么设置为2nd 参数,目前为null

我为onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle) 找到了answers,但该方法不适用于Dialog

伪造null 之类的(ViewGroup) null 对我来说不是一种选择

我的对话框

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);
    }
}

错误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);

有什么想法吗?提前致谢!

解决方案

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);
    }
}

【问题讨论】:

    标签: java android android-layout null


    【解决方案1】:

    使用这个

    setContentView(R.layout.my_dialog);
    

    不是这个

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

    示例代码

    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();
            }
        }
    }
    

    然后像这样创建你的对话框

    MyDialog myDialog = new MyDialog(this);
    myDialog.show();
    

    【讨论】:

    • 谢谢 Nilesh,但是我怎样才能得到 View view 呢?我需要它来控制Dialog 中的Button
    • @Mr.B.您可以直接findViewByIdonCreate() 内部执行类似Button yes = findViewById(R.id.btn_yes); 的方法,而不是根据您的要求执行操作
    • @NileshRathod 问题在于,这两种方法都是正确的。他的问题可能在上下文中,getContext() 可能返回 null。只需在构造函数中从您的活动中传递上下文,您的代码就可以工作。有时代码不起作用,因为 inflate() 方法中需要所有 3 个参数,这里只有两个。
    • @Mr.B.尝试更新答案先做setContentView() 然后做findViewById()
    • @Mr.B.在设置视图之前,您无法初始化按钮。所以你的代码可以工作,当你把所有代码移到setContentView()下面时,这应该总是在super方法之后。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多