我已经编写了我的第一个 Kotlin 和 Android 应用程序,但我在这个应用程序上遇到了很多崩溃。
所有这些都与关键字相关lateinit。
我遇到如下崩溃:
Caused by e.y: lateinit property coordinator has not been initialized
和:
Fatal Exception: java.lang.RuntimeException
Unable to start activity ComponentInfo{app.myapp/mypackage.myapp.Controllers.MainActivity}: e.y: lateinit property coordinator has not been initialized
Caused by e.y
lateinit property coordinator has not been initialized
myapp.com.myapp.Controllers.Fragments.Parents.MyParentFragment.getCoordinator
Run Code Online (Sandbox Code Playgroud)
例如,最后一个跟踪与我在片段上设置的变量相关,如下所示:
fun newInstance(mainObject: MyObject, anotherObject: AnotherObject, coordinator: FragmentCoordinator): MyFragment {
val fragment = MyFragment()
fragment.mainObject = mainObject
fragment.anotherObject = ticket
fragment.coordinator = coordinator
return fragment
}
Run Code Online (Sandbox Code Playgroud)
片段侧面看起来像:
class MyFragment: MyParentFragment() {
companion object …Run Code Online (Sandbox Code Playgroud) 我正在尝试从我的阵列中删除重复的对象.
我的自定义由两个双重组成:x和y.
我想要做的是删除重复((x && y)==(x1 && y1))如果x == x1我想保留具有更高y的对象.
ArrayList<MyObject> list = [x(0),y(0)], [x(0),y(0)], [x(0.5),y(0.5], [x(0.5),y(0.6)], [x(1),y(1)];
ArrayList<MyObject> results = [x(0),y(0)], [x(0.5),y(0.6)], [x(1),y(1)];
Run Code Online (Sandbox Code Playgroud)
我试图实现equals方法,但我不知道如何使用它:
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof MyObject)) {
return false;
}
return (this.x == ((MyObject)obj).x);
}
Run Code Online (Sandbox Code Playgroud)
始终使用Collections.sort by x对list进行排序.
谢谢大家.