简体   繁体   English

如何使用Kotlin在Android Studio中引用TimerPicker

[英]How to reference a timerPicker in android studio using kotlin

When this program runs on the emulator it works, however, when I run it on my phone the timePicker is referenced as null. 当该程序在模拟器上运行时,它可以工作,但是,当我在手机上运行该程序时, timePicker被引用为null。

An emulator running API 26 can run it fine, however, at API 28 it does not work. 运行API 26的仿真器可以正常运行,但是在API 28上它不起作用。 So I think It might be something with API 28 (which also is what my phone runs) 所以我认为这可能与API 28有关(这也是我的手机运行的东西)

class MainActivity : AppCompatActivity() {

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

        val picker = findViewById<TimePicker>(R.id.timePicker)

        val t = picker == null // Is true on phone and is false on emulator

        Toast.makeText(this@MainActivity, t.toString(), Toast.LENGTH_LONG).show()
        //picker.setIs24HourView(true)

        val activity = findViewById<EditText>(R.id.activityName)
        val mCallApiButton = findViewById<Button>(R.id.submit)
        mCallApiButton?.setOnClickListener{
            val time = GetTime(picker)

            PostActivity(time, activity.text.toString()) // Function that does other stuff
        }
    }
}

I need to reference the timePicker so that I can set it to 24H. 我需要引用timePicker,以便将其设置为24H。

Here is a look at the layout file: 看一下布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" app:layout_constraintTop_toTopOf="parent">
        <TimePicker android:id="@+id/timePicker"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:timePickerMode="clock"/>
        <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:id="@+id/activityName"
                android:singleLine="true" android:hint="Activity" android:textSize="18sp"
                android:textAlignment="center" android:autofillHints="Activity"/>
        <Button
                android:text="Submit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" android:id="@+id/submit"/>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Screenshot of the emulator: (Which is how it should look) 模拟器的屏幕截图:(应如何显示) 在此处输入图片说明

Screenshot of the phone: 手机屏幕截图: 在此处输入图片说明

This is because I use API 28 and not 27 and under. 这是因为我使用的是API 28,而不是27及以下版本。 The answer can found here: https://github.com/xamarin/Xamarin.Forms/issues/5159 答案可以在这里找到: https : //github.com/xamarin/Xamarin.Forms/issues/5159

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM