无法使用Kotlin从自定义对话框获取文本

f45qwnt8  于 12个月前  发布在  Kotlin
关注(0)|答案(1)|浏览(128)

我有一个自定义对话框,要求两个文本输入,我的目标是从textview的文本时,对话框的确定选项被按下,但到目前为止,我甚至不能访问名为userName的textView,我不能找出我做错了什么.与下面的代码我得到这个错误“@layout/dialog_create_task_type does not contain a declaration with id userName”
这是我的DialogFragment:

class TaskTypeCreationDialog : DialogFragment() {

    internal lateinit var listener: TaskTypeCreationDialogListener

    interface TaskTypeCreationDialogListener {
        fun onDialogPositiveClick(dialog: DialogFragment, taskName: String)
        fun onDialogNegativeClick(dialog: DialogFragment)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            // Get the layout inflater.
            val inflater = requireActivity().layoutInflater;
            val inflatedView = inflater.inflate(R.layout.dialog_create_task_type, null)
            // Inflate and set the layout for the dialog.
            // Pass null as the parent view because it's going in the dialog
            // layout.
            val userName = inflatedView.findViewById<TextView>(R.id.userName)

            builder.setView(inflatedView)
                // Add action buttons.
                .setPositiveButton("ok"
                ) { dialog, id ->
                    listener.onDialogPositiveClick(this, "test")
                    dialog?.cancel()
                }
                .setNegativeButton("cancel"
                ) { dialog, id ->
                    getDialog()?.cancel()
                }
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        // Verify that the host activity implements the callback interface.
        try {
            // Instantiate the NoticeDialogListener so you can send events to
            // the host.
            listener = context as TaskTypeCreationDialogListener
        } catch (e: ClassCastException) {
            // The activity doesn't implement the interface. Throw exception.
            throw ClassCastException((context.toString() +
                    " must implement NoticeDialogListener"))
        }
    }

}

字符串
这是我的xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/username"
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="first text" />
    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="16dp"
        android:fontFamily="sans-serif"
        android:hint="second text"/>
</LinearLayout>


我也尝试过使用绑定,但它崩溃了,“java.lang.NullPointerException:Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference“,代码如下:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            // Get the layout inflater.
            val inflater = requireActivity().layoutInflater;
            // Inflate and set the layout for the dialog.
            // Pass null as the parent view because it's going in the dialog
            // layout.
            val v = inflater.inflate(R.layout.dialog_create_task_type, null)
            builder.setView(v)
                // Add action buttons.
                .setPositiveButton("ok",
                    DialogInterface.OnClickListener { dialog, id ->
                        val binding = TaskTypeLayoutBinding.bind(v)
                        listener.onDialogPositiveClick(this, binding.userName.text.toString())
                        dialog?.cancel()
                    })
                .setNegativeButton("cancel",
                    DialogInterface.OnClickListener { dialog, id ->
                        getDialog()?.cancel()
                    })
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }```

wgmfuz8q

wgmfuz8q1#

我发现我的错误是坚持在onViewDialog()上获取TextView,现在我已经将此逻辑移动到onViewCreated(),我没有问题获得我需要的视图,我不确定我的错误是否会帮助任何人,但我会留下我的问题以防万一。
我对代码做了一些修改,现在是这样的:

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val inflater = requireActivity().layoutInflater;
    val inflatedView = inflater.inflate(R.layout.dialog_create_task_type, null)
    // Inflate and set the layout for the dialog.
    // Pass null as the parent view because it's going in the dialog
    // layout.
return inflatedView
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    binding = DialogCreateTaskTypeBinding.bind(view)
    setupClickListeners(view)
}

private fun setupClickListeners(view: View) {
    val builder = activity?.let { AlertDialog.Builder(it) }
    if (builder != null) {
        builder.setView(view)
            // Add action buttons.
            .setPositiveButton("ok"
            ) { dialog, id ->
                listener.onDialogPositiveClick(this, binding.username.text.toString())
                dialog?.cancel()
            }
            .setNegativeButton("cancel"
            ) { dialog, id ->
                getDialog()?.cancel()
            }
        builder.create()
    }
}

字符串

相关问题