У меня есть некоторый код Java, который использует отражение для создания нового объекта. Новый объект - это объект, который я кодировал в Kotlin. Код работает нормально, если только в моем Kotlin классе я не использую init
, чтобы попытаться инициализировать некоторые переменные.
например:
private BaseClass constructViewHolderClass(View itemView, int viewType) {
View itemView = ...
Class<? extends BaseClass> baseClass = ChildBaseClass.class;
Constructor<? extends BaseClass> ctor;
ctor = baseClass.getConstructor(View.class);
return (BaseClass) ctor.newInstance(new Object[]{itemView});
}
Kotlin класс, который работает:
class ChildBaseClass(itemView: View) : BaseClass(itemView) {
var imageView: ImageView = itemView.findViewById(R.id.image_view)
var circularProgressDrawable = CircularProgressDrawable(itemView.context)
}
Kotlin класс, который НЕ работает:
class ChildBaseClass(itemView: View) : BaseClass(itemView) {
var imageView: ImageView = itemView.findViewById(R.id.image_view)
var circularProgressDrawable = CircularProgressDrawable(itemView.context)
init {
circularProgressDrawable.strokeWidth = 5f
circularProgressDrawable.centerRadius = 30f
circularProgressDrawable.start()
}
}
Исключение составляет java.lang.IllegalArgumentException: class java.lang.reflect.InvocationTargetException
Stacktrace:
Process: com.project.acme, PID: 12668
java.lang.IllegalArgumentException: class java.lang.reflect.InvocationTargetException
at com.project.acme.extlibs.myrecyclerview.MYRecyclerViewAdapter.constructViewHolderClass(MYRecyclerViewAdapter.java:844)
at com.project.acme.extlibs.myrecyclerview.MYRecyclerViewAdapter.onCreateViewHolder(MYRecyclerViewAdapter.java:830)
at com.project.acme.extlibs.myrecyclerview.MYRecyclerViewAdapter.onCreateViewHolder(MYRecyclerViewAdapter.java:16)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:20822)
at android.view.ViewGroup.layout(ViewGroup.java:6401)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20822)
at android.view.ViewGroup.layout(ViewGroup.java:6401)
at androidx.swiperefreshlayout.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:625)
at android.view.View.layout(View.java:20822)
at android.view.ViewGroup.layout(ViewGroup.java:6401)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20822)
at android.view.ViewGroup.layout(ViewGroup.java:6401)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20822)
at android.view.ViewGroup.layout(ViewGroup.java:6401)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:20822)
at android.view.ViewGroup.layout(ViewGroup.java:6401)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20822)
at android.view.ViewGroup.layout(ViewGroup.java:6401)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:20822)
at android.view.ViewGroup.layout(ViewGroup.java:6401)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20822)
at android.view.ViewGroup.layout(ViewGroup.java:6401)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:20822)
at android.view.ViewGroup.layout(ViewGroup.java:6401)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)