![]() ![]() If you choose a non-null type, the compiler will emit an assertion upon assignment. Val notNull: String = item // allowed, may fail at runtime For example framework only calls onStart() and onStop() on main (UI) thread, so you can feel confident to access private properties if the property is only accessed by those lifecycle methods.Val nullable: String? = item // allowed, always works This Thread Confinement also applies to all the typical lifecycle methods in Android. If framework developers have to worry about multi-threading environment, the codebase will become very difficult to maintain and the overall performance will decrease because of all the required synchronizations. ![]() Now you should understand why Android framework has this limitation. Let’s change the sample code to demonstrate the idea:Ī$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.Īt (ViewRootImpl.java:6556)Īt (ViewRootImpl.java:907)Īt (View.java:18722)Īt .requestLayout(ConstraintLayout.java:1959)Īt 7.(AppCompatTextViewAutoSizeHelper.java:625)Īt 7.(AppCompatTextViewAutoSizeHelper.java:598)Īt 7.(AppCompatTextHelper.java:373)Īt 7.(AppCompatTextHelper.java:355)Īt 7.(AppCompatTextView.java:191)Īt $onCreate$thread$1.run(DelegateActivity.kt:29) You might think that’s a stupid idea, but it is actually a legit solution when you can limit “how your property is accessed”. Yes the first solution is simply just put !! in front of your property when accessing it! ![]() ![]() Fun main ( args : Array ) # Console output : name : Eric name : null name : null name : null name : null name : Eric name : Eric name : null Process finished with exit code 130 ( interrupted by signal 2 : SIGINT )įrom the console output we can see the null check just cannot guarantee name won’t be null when you access it exactly as what IDE tries to warn you. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |