Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 创建自定义toast时,findViewById()不能为null_Android_Kotlin - Fatal编程技术网

Android 创建自定义toast时,findViewById()不能为null

Android 创建自定义toast时,findViewById()不能为null,android,kotlin,Android,Kotlin,我有下面的函数来显示自定义的toast fun showCustomToast() { val inflater = layoutInflater val container: ViewGroup = findViewById(R.id.toastContainer) val layout: ViewGroup = inflater.inflate(R.layout.custom_toast, container) as ViewGroup

我有下面的函数来显示自定义的toast

fun showCustomToast() {
        val inflater = layoutInflater
        val container: ViewGroup = findViewById(R.id.toastContainer)
        val layout: ViewGroup = inflater.inflate(R.layout.custom_toast, container) as ViewGroup
        val text: TextView = layout.findViewById(R.id.textView)
        text.text = "This is a custom toast"
        with (Toast(applicationContext)) {
            setGravity(Gravity.CENTER_VERTICAL, 0, 0)
            duration = Toast.LENGTH_LONG
            view = layout
            show()
        }
}
在运行应用程序时,我在
setContentView(R.layout.activity\u main)
下方的主活动上调用此函数,出现以下错误

我做错了什么

我遇到过这样的情况,但没有任何帮助。

函数
findViewById()
将搜索活动布局中的视图,而不是您可能声明的任何其他特定视图。
您的容器是否在另一个
视图中

函数
findViewById()
将搜索活动布局中的视图,而不是您可能声明的任何其他特定视图。 您的容器是否在另一个
视图中

在您的评论中,您编写的
toastcainer
位于
custom\u toast
布局中。在当前布局中找不到它(假定它是活动)

实际上,你不需要一个容器来装充气机,而且因为烤面包片还没有显示出来,所以也不会有任何真正的容器可用。您可以用以下内容替换这两行

val layout = inflater.inflate(R.layout.custom_toast, null)
在您的评论中,您编写的
toastcainer
位于
custom\u toast
布局中。在当前布局中找不到它(假定它是活动)

实际上,你不需要一个容器来装充气机,而且因为烤面包片还没有显示出来,所以也不会有任何真正的容器可用。您可以用以下内容替换这两行

val layout = inflater.inflate(R.layout.custom_toast, null)

这个(R.id.toastcainer)id确实存在吗?是的,它存在!IllegalStateException错误是一个运行时错误,如果toastContainer不存在@PrajwalWTry正在清理项目中哪个布局是R.layout.custom_toast@laaltodooes中的
toastContainer
,它将无法编译此(R.id.toastContainer)id实际存在?是的,它存在!IllegalStateException错误是一个运行时错误,如果toastContainer不存在@PrajwalWTry正在清理项目,它将无法编译。在R.layout.custom_toast@laaltoI中,哪个布局是
toastContainer
,哪个布局是。那么,我如何从主活动中设置自定义toast?我有一个活动布局和不同的toast布局。那么,如何从主要活动中设置自定义吐司
val layout = inflater.inflate(R.layout.custom_toast, null)