[英]Android findViewById() returns null when trying to make a utility/helper class
I am currently developing an app and I am trying to make a Utils file with some general functions that I can use throughout my application to avoid having to copy/paste the same function many times.我目前正在开发一个应用程序,我正在尝试制作一个具有一些通用功能的 Utils 文件,我可以在整个应用程序中使用这些功能,以避免多次复制/粘贴相同的 function。 I have a Utils class file and I am passing the current Activity into that class.
我有一个 Utils class 文件,我正在将当前活动传递给该 class。 For some reason, when I call findViewById() in init, it returns null and the app crashes.
出于某种原因,当我在 init 中调用 findViewById() 时,它返回 null 并且应用程序崩溃。 I have tried logging the activity that is being passed into the Utils class, and to my eyes it appears like the right activity, so I am not sure why it is failing.
我已经尝试记录正在传递到 Utils class 的活动,在我看来它似乎是正确的活动,所以我不确定它为什么会失败。 I will show the relevant code below:
我将在下面显示相关代码:
Utils.kt (There is more, but it isn't relevant to the question) Utils.kt(还有更多,但与问题无关)
class Utils(activity: Activity) {
private var currentActivity: Activity
private var fragmentManager: FragmentManager
private var topAppBar: MaterialToolbar
val activitiesList = listOf(
"recipes", "budget", "inventory", "customers",
"reports"
)
init {
currentActivity = activity
println(currentActivity)
fragmentManager = (activity as AppCompatActivity).supportFragmentManager
topAppBar = currentActivity.findViewById(R.id.top_app_bar)
}
}
MainActivity.kt (Again there is more, but not relevant) MainActivity.kt(还有更多,但不相关)
class MainActivity : AppCompatActivity() {
private lateinit var drawerLayout: DrawerLayout
private lateinit var topAppBar: MaterialToolbar
private lateinit var utils: Utils
private val fragmentManager = supportFragmentManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fragmentManager.beginTransaction().replace(R.id.frame_layout, HomeFragment()).commit()
utils = Utils(this@MainActivity)
topAppBar = findViewById(R.id.top_app_bar)
drawerLayout = findViewById(R.id.drawer_layout)
val navigationView: NavigationView = findViewById(R.id.navigation_view)
The error comes from the Utils.kt file in the last line of actual code where I try to assign topAppBar to the view of id top_app_bar, but it returns null.该错误来自实际代码最后一行中的 Utils.kt 文件,在该文件中我尝试将 topAppBar 分配给 id top_app_bar 的视图,但它返回 null。 My println (yes I know I should be using Log.i but println is quicker to type) in the Utils class returns com.example.bakingapp.MainActivity@501533d, which is what I would expect if I am understanding what is happening correctly.
我在 Utils class 中的 println(是的,我知道我应该使用 Log.i,但 println 输入速度更快)返回 com.example.bakingapp.MainActivity@501533d,如果我理解正确发生的事情,这就是我所期望的。 Any insight as to why I can't find the views would be appreciated.
任何关于我为什么找不到这些观点的见解都将不胜感激。
I figured out the problem.我解决了这个问题。 It is a scope issue.
这是一个 scope 问题。 I am trying to find the id of a view outside of the current Activity/Fragment I am looking at.
我试图在我正在查看的当前活动/片段之外找到视图的 ID。 I realized then a lot of my code could/should be moved into the Fragment I was trying to target and this has fixed my issues.
然后我意识到我的很多代码可以/应该移动到我试图定位的片段中,这解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.