开发者

Should findviewbyid be called in UI thread?

The question is: should I call Activity.findViewById in the UI thread only?

The reason I'm asking is that I call 开发者_开发技巧it from an arbitrary thread and it works flawlessly. But then I got an exception from one of the users: findViewById fails with NPE somewhere in findViewTraversal.

So, can it be the cause?


the Android UI toolkit is not thread-safe and must always be manipulated on the UI thread.

Taken from Painless threading.

Because it's not thread-safe, you don't know what will happen if you use the API in another thread. It might work, it might not work, it might work most of the time, might never work or it might work and breaks other things. You can't tell.

Thus, if you want a correct application, you must do this in the UI thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜