无法推断此参数的类型,请明确指定它?

时间:2019-08-29 18:22:21

标签: kotlin types

我最近使用Kotlin开发了android应用,但出现以下错误

无法推断此参数的类型。请明确指定

下面的错误截图 enter image description here

在我遇到错误的班级下

class AddBookingActivity : AppCompatActivity() {
    @BindView(R.id.attendees_recycler_view)
    internal var mAttendeeRecyclerView: RecyclerView? = null

    @BindView(R.id.start_time_edit_text)
    internal var mStartTime: EditText? = null
    @BindView(R.id.end_time_edit_text)
    internal var mEndTime: EditText? = null

    private var mAttendeeName: EditText? = null
    private var mAttendeeEmail: EditText? = null
    private var mAttendeePhoneNumber: EditText? = null

    private var mAttendeeAdapter: AttendeeAdapter? = null
    private var mAlertDialog: AlertDialog? = null

    private val mAttendeeItemList = ArrayList<AttendeeItem>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_booking)

        ButterKnife.bind(this)

        mAttendeeRecyclerView!!.layoutManager = LinearLayoutManager(this)
        mAttendeeAdapter = AttendeeAdapter(this)
        mAttendeeAdapter!!.setAttendeeList(mAttendeeItemList)
        mAttendeeRecyclerView!!.adapter = mAttendeeAdapter

    }

    @OnClick(R.id.toolbar_back_button)
    internal fun onBackButtonClicked() {
        onBackPressed()
    }

    @OnClick(R.id.start_time_edit_text)
    internal fun onStartTimeClicked() {
        showTimePickerDialog(mStartTime)
    }

    @OnClick(R.id.end_time_edit_text)
    internal fun onEndTimeClicked() {
        showTimePickerDialog(mEndTime)
    }

    @OnClick(R.id.fab_add_attendee)
    internal fun onAddAttendeeClicked() {

        val inflater = this.layoutInflater
        val dialogView = inflater.inflate(R.layout.add_attendee_view, null)

        mAttendeeName = dialogView.findViewById(R.id.attendee_name)
        mAttendeeEmail = dialogView.findViewById(R.id.attendee_email)
        mAttendeePhoneNumber = dialogView.findViewById(R.id.attendee_phone_number)

        mAlertDialog = AlertDialog.Builder(this, R.style.AlertDialog)
            .setTitle("Input attendee details")
            .setPositiveButton("Add") { dialog, which ->
                val item = AttendeeItem()
                item.name = mAttendeeName!!.text.toString()
                item.email = mAttendeeEmail!!.text.toString()
                item.phoneNumber = mAttendeePhoneNumber!!.text.toString()

                mAttendeeItemList.add(item)
                mAttendeeAdapter!!.setAttendeeList(mAttendeeItemList)
                mAttendeeAdapter!!.notifyItemInserted(mAttendeeItemList.size)
            }
            .setNegativeButton("Cancel", null)
            .setView(dialogView).create()

        mAlertDialog!!.show()
    }

    private fun showTimePickerDialog(editText: EditText?) {
        val myCalender = Calendar.getInstance()
        val hour = myCalender.get(Calendar.HOUR_OF_DAY)
        val minute = myCalender.get(Calendar.MINUTE)


        @SuppressLint("DefaultLocale")
        val myTimeListener = { view, hourOfDay, minute1 ->
            if (view.isShown()) {
                myCalender.set(Calendar.HOUR_OF_DAY, hourOfDay)
                myCalender.set(Calendar.MINUTE, minute1)
                editText!!.setText(String.format(String.format("%s:%s", hourOfDay.toString(), minute1.toString())))
            }
        }
        val timePickerDialog = TimePickerDialog(this, android.R.style.Theme_Holo_Light_Dialog_NoActionBar, myTimeListener, hour, minute, true)
        timePickerDialog.setTitle("Choose hour:")
        timePickerDialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)
        timePickerDialog.show()
    }
}

1 个答案:

答案 0 :(得分:0)

您必须指定接口来帮助Kotlin编译器:

@SuppressLint("DefaultLocale")
val myTimeListener = TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute1 ->
    if (view.isShown()) {
        myCalender.set(Calendar.HOUR_OF_DAY, hourOfDay)
        myCalender.set(Calendar.MINUTE, minute1)
        editText!!.setText(String.format(String.format("%s:%s", hourOfDay.toString(), minute1.toString())))
    }
}