连续两次调用 Activity.setContentView() 的副作用
在我的应用程序中,我需要从 onCreate()
和 WebChromeClient.onCompletion()
调用 findViewbyId()
。避免 findViewbyId()
返回 null 在调用 findViewbyId()
之前,我求助于调用 Activity.setContentView(int viewResID)
。
它有效,但现在我想知道我是否可能在我的代码中创建了一个“定时炸弹”。
做这样的事情时我需要记住什么注意事项吗?
In my app I needed to call findViewbyId()
from both onCreate()
and from WebChromeClient.onCompletion()
. To avoid findViewbyId()
returning null I resorted to calling Activity.setContentView(int viewResID)
before calling findViewbyId()
.
It works, but now I am wondering whether I may have created a "time-bomb" in my code down the road.
Are there any caveats that I need to keep in mind when doing something like this?
这样做是没有问题的。你必须重新初始化引用(每个findViewById都需要再次调用),如果你这样做很多次可能会很麻烦,但它根本不是一个定时炸弹。
There is no problem in doing so. You have to reinitialize references (every findViewById needs to be called again) and it might be troublesome if you do it A LOT of times, but it is not a time bomb at all.