在自定义Dialog Android Kotlin中,视图为null

时间:2018-12-24 13:56:01

标签: android kotlin nullpointerexception android-dialog

我在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()
}

当我尝试运行我的应用并显示我的自定义Dialog时,在setDate方法中,calendarView为null。 这是我的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我是科特林的初学者。 谢谢

2 个答案:

答案 0 :(得分:0)

我猜在调用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)
}

答案 1 :(得分:0)

您可以将代码放入onCreateDialog而不是onCreateView中。 Android建议使用DialogFragment,因为它具有生命周期意识