首页 > 解决方案 > View 在自定义对话框 Android Kotlin 中为空

问题描述

我在 kotlin 类中创建了自定义对话框。
这是我的代码:

class SPCalendarDialog(context: Context) : Dialog (context,R.style.DialogTheme) {
    private var listener: OnSPCalendarListener? = null
    private var year: Int = 0
    private var month: Int = 0
    private var dayOfMonth: Int = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setCancelable(true)
        setCanceledOnTouchOutside(true)
        setContentView(R.layout.sp_calendar_dialog)
        val calendar = Calendar.getInstance()
        year = calendar.get(Calendar.YEAR)
        month = calendar.get(Calendar.MONTH)
        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)
        calendarView.descendantFocusability = DatePicker.FOCUS_BLOCK_DESCENDANTS
        calendarView.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)) { _, y, m, dM ->
            year = y
            month = m
            dayOfMonth = dM
        }
    }
    fun setDate(date: Long) {
        val calendar = Calendar.getInstance()
        if (date != 0L)
            calendar.timeInMillis = date
        calendarView.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)) { _, y, m, dM ->
            year = y
            month = m
            dayOfMonth = dM
        }
    }
    interface OnSPCalendarListener {
        fun onCalendarChangeListener(year: Int, month: Int, dayOfMonth: Int)
    }
    fun setListener(listener: OnSPCalendarListener) {
        this.listener = listener
    }

}

我这样称呼这个类。

private fun showSPCalendarDialog(view: View) {
    var spCalendarDialog = SPCalendarDialog(view.context)
    var birthDate: Long = 0

    spCalendarDialog.setListener(object : SPCalendarDialog.OnSPCalendarListener {
        override fun onCalendarChangeListener(year: Int, month: Int, dayOfMonth: Int) {
        }
    })
    spCalendarDialog.setDate(birthDate)
    spCalendarDialog.show()
}

当我尝试运行我的应用程序并显示我的自定义对话框时,在 setDate 方法中,calendarView 为空。这是我的 xml 代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">


<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/dimen_p_30"
    android:layout_marginRight="@dimen/dimen_p_30"
    app:cardBackgroundColor="@color/white"
    android:layout_centerInParent="true"
    app:cardCornerRadius="@dimen/dimen_p_15">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <DatePicker
            android:id="@+id/calendarView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:calendarViewShown="false"
            android:datePickerMode="spinner"
            android:descendantFocusability="blocksDescendants" />


        <RelativeLayout
            android:id="@+id/dialog_ok_button"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dimen_p_46"
            android:background="@color/light_blue">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:fontFamily="@font/myriad_geo_mtavruli_bold"
                android:text="@string/approve"
                android:textColor="@color/white"
                android:textSize="@dimen/dimen_p_14" />

        </RelativeLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>

我正在使用 kotlinx 从没有 findById 的 xml 中查找视图

import kotlinx.android.synthetic.main.sp_calendar_dialog.*

谁能告诉我,我做错了什么?ps 我是 Kotlin 的初学者。谢谢

标签: androidkotlinnullpointerexceptionandroid-dialog

解决方案


我猜在调用的时候setDate calendarView还没有初始化。显示对话框后尝试调用setDate一段时间:

private fun showSPCalendarDialog(view: View) {
    var spCalendarDialog = SPCalendarDialog(view.context)
    var birthDate: Long = 0

    spCalendarDialog.setListener(object : SPCalendarDialog.OnSPCalendarListener {
        override fun onCalendarChangeListener(year: Int, month: Int, dayOfMonth: Int) {
        }
    })
    spCalendarDialog.show()
    view.postDelayed({ spCalendarDialog.setDate(birthDate) }, 500)
}

推荐阅读