Toast可以在Java活动中工作,但不能在kotlin中工作
我有一个自定义toast,它在java活动中工作,但在kotlin中不工作。在kotlin活动中,它抛出以下错误:Toast可以在Java活动中工作,但不能在kotlin中工作,java,android,kotlin,Java,Android,Kotlin,我有一个自定义toast,它在java活动中工作,但在kotlin中不工作。在kotlin活动中,它抛出以下错误: kotlin.TypeCastException: null cannot be cast to non-null type android.view.ViewGroup 在这条线上 val layout = inflater.inflate(R.layout.custom_toast, findViewById<View>(R.id.custom_to
kotlin.TypeCastException: null cannot be cast to non-null type android.view.ViewGroup
在这条线上
val layout = inflater.inflate(R.layout.custom_toast,
findViewById<View>(R.id.custom_toast_container) as ViewGroup)
android studio如何将其转换为kotlin:
val inflater = layoutInflater
val layout = inflater.inflate(R.layout.custom_toast,
findViewById<View>(R.id.custom_toast_container) as ViewGroup)
val text = layout.findViewById<View>(R.id.text) as TextView
text.text = "Already reported"
val toast = Toast(context)
toast.setGravity(Gravity.BOTTOM, 0, 145)
toast.duration = Toast.LENGTH_LONG
toast.view = layout
toast.show()
val充气机=充气机
val布局=充气机。充气(R.layout.custom_toast,
findViewById(R.id.custom\u toast\u容器)作为视图组)
val text=layout.findviewbyd(R.id.text)作为文本视图
text.text=“已报告”
val toast=toast(上下文)
toast.setGravity(Gravity.BOTTOM,0,145)
toast.duration=toast.LENGTH\u LONG
toast.view=布局
吐司
我在这里做错了什么?我认为这是因为
findViewById
返回nullable,但您需要强制转换为非null类型。在这里,我稍微修改了您的代码:
val inflater = layoutInflater
val layout = inflater.inflate(R.layout.custom_toast,
findViewById<View>(R.id.custom_toast_container) as ViewGroup?)
val text = layout?.findViewById<View>(R.id.text) as TextView?
text?.text = "Already reported"
val toast = Toast(context)
toast.setGravity(Gravity.BOTTOM, 0, 145)
toast.duration = Toast.LENGTH_LONG
toast.view = layout
toast.show()
val充气机=充气机
val布局=充气机。充气(R.layout.custom_toast,
findViewById(R.id.custom_toast_container)作为视图组?)
val text=layout?.findViewById(R.id.text)作为文本视图?
text?.text=“已报告”
val toast=toast(上下文)
toast.setGravity(Gravity.BOTTOM,0,145)
toast.duration=toast.LENGTH\u LONG
toast.view=布局
吐司
我希望这会对您有所帮助。我认为这是因为
findViewById
返回null可值,但您需要转换为非null类型。在这里,我稍微修改了您的代码:
val inflater = layoutInflater
val layout = inflater.inflate(R.layout.custom_toast,
findViewById<View>(R.id.custom_toast_container) as ViewGroup?)
val text = layout?.findViewById<View>(R.id.text) as TextView?
text?.text = "Already reported"
val toast = Toast(context)
toast.setGravity(Gravity.BOTTOM, 0, 145)
toast.duration = Toast.LENGTH_LONG
toast.view = layout
toast.show()
val充气机=充气机
val布局=充气机。充气(R.layout.custom_toast,
findViewById(R.id.custom_toast_container)作为视图组?)
val text=layout?.findViewById(R.id.text)作为文本视图?
text?.text=“已报告”
val toast=toast(上下文)
toast.setGravity(Gravity.BOTTOM,0,145)
toast.duration=toast.LENGTH\u LONG
toast.view=布局
吐司
我希望这会对你有所帮助。避免findviewbyd的简单方法 在build.gradle(应用程序)文件中,添加以下行
apply plugin: 'kotlin-android-extensions'
现在,您可以在.kt文件中编写以下代码
val inflater = layoutInflater
var inflatedFrame = inflater.inflate(R.layout.demo, null)
inflatedFrame.text.text = "Already reported"
val toast = Toast(context)
toast.setGravity(Gravity.BOTTOM, 0, 145)
toast.duration = Toast.LENGTH_LONG
toast.view = inflatedFrame
toast.show()
避免findViewById的简单方法 在build.gradle(应用程序)文件中,添加以下行
apply plugin: 'kotlin-android-extensions'
现在,您可以在.kt文件中编写以下代码
val inflater = layoutInflater
var inflatedFrame = inflater.inflate(R.layout.demo, null)
inflatedFrame.text.text = "Already reported"
val toast = Toast(context)
toast.setGravity(Gravity.BOTTOM, 0, 145)
toast.duration = Toast.LENGTH_LONG
toast.view = inflatedFrame
toast.show()
它说findViewById(R.id.custom_toast_container)
为空,因此它应该作为视图组?
而不是视图组
它说findViewById(R.id.custom_toast_container)
为空,因此它应该作为视图组?
而不是视图组