Kotlin问题:类中的TimePickerDialog.OnTimeSetListener(输出:2个值Long&String)

时间:2019-03-10 18:17:21

标签: class kotlin

三个月后,我仍然没有找到解决方案,我需要您的帮助。

语言: 科特林 (Android Studio 3.3.2 版本号AI-182.5107.16.33.5314842,于2019年2月16日建立 JRE:1.8.0_152-release-1248-b01 amd64 JVM:JetBrains s.r.o的OpenJDK 64位服务器VM Windows 10 10.0)

目标/目标: 类中的TimePickerDialog.OnTimeSetListener(输出:2个值,Long&String)

问题: 我无法真正描述问题,我只有此Kotlin错误消息。 上下文:此->类型不匹配。必需:上下文!找到:MyClass

我的代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<TextView
        android:text="@string/textview"
        android:layout_width="125dp"
        android:layout_height="58dp"
        android:id="@+id/textView" android:layout_marginTop="60dp"
        app:layout_constraintTop_toTopOf="parent" 
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="40dp"/>
<Button
        android:text="@string/button"
        android:layout_width="113dp"
        android:layout_height="58dp"
        android:id="@+id/button" android:layout_marginTop="60dp"
        app:layout_constraintTop_toTopOf="parent"         
        app:layout_constraintStart_toEndOf="@+id/textView"
        android:layout_marginStart="56dp"/>

</android.support.constraint.ConstraintLayout>

MainActivity.kt

package com.terminal_io.goodapp

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity()
{

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

    var myOutput = MyClass (31415926535, "Test")

    button.setOnClickListener()
    {
        myOutput.myTime()
        textView.text = myOutput.myStringInfo
    }
  }
}

科特琳班

package com.terminal_io.goodapp

import android.app.TimePickerDialog
import java.sql.Time
import java.text.SimpleDateFormat
import java.util.*

class MyClass (LongInfo: Long, StringInfo : String)
{
var myLongInfo = LongInfo
var myStringInfo = StringInfo

fun myTime() : Long
{
    var cal = Calendar.getInstance()

    var timeSetListener = TimePickerDialog.OnTimeSetListener { timePicker, 
  hour, minute ->
        cal.set(Calendar.HOUR_OF_DAY, hour)
        cal.set(Calendar.MINUTE, minute)

        myStringInfo = SimpleDateFormat("HH:mm").format(cal.time)
    }

    var Hour = Calendar.HOUR_OF_DAY.toLong()
    var Minute = Calendar.MINUTE.toLong()

    var h: Long = Hour * 3600000 // Hour in millisecs
    var m: Long = Minute * 60000 // Minute in millisecs
    myLongInfo = h + m

    TimePickerDialog(this, timeSetListener, // ??? --> context: this --> 
    // Type mismatch. Required: Context! Found: MyClass
        cal.get(Calendar.HOUR_OF_DAY),
        cal.get(Calendar.MINUTE),
        true
    ).show()

    return myLongInfo; myStringInfo // ??? -> myStringInfo --> Unreachable 
    // code
    }
}

0 个答案:

没有答案