我是Kotlin的新手,我不知道自己做错了什么。任何人都可以帮助我吗?
片段代码:
fun startViewAnimations() {
rippleBack?.startRippleAnimation()
}
活动代码:
override fun onPageSelected(position: Int) {
when(position) {
0 -> (pagerAdapter.getItem(position) as SearchFragment).startViewAnimations()
}
}
以下是错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.beeland.consumer, PID: 5892
java.lang.IllegalStateException: txt5Km must not be null
at com.beeland.consumer.fragment.appintro.SearchFragment$startViewAnimations$1$1.run(SearchFragment.kt:56)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
答案 0 :(得分:1)
如果您使用Kotlin Android Extensions和片段,请考虑您只能在onViewCreated
及更高版本中通过其ID调用视图。如果你以前打电话,你会看到NPE。
如果您不使用KAE,可以像这样初始化片段中的视图:
class MyFragment: Fragment() {
// other methods
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val myTextView = view.findViewById<TextView>(R.id.my_text_view)
// now you can do something with views
}
// other methods
}