Reputation: 43
I converted a Java class to kotlin and am getting the mentioned the error Projections are not allowed for immediate arguments of a supertype
The Java class is
public class RecipeViewHolder extends ParentViewHolder {
private static final float INITIAL_POSITION = 0.0f;
private static final float ROTATED_POSITION = 180f;
@NonNull
private final ImageView mArrowExpandImageView;
private TextView mRecipeTextView;
private TextView position;
private TextView total;
public RecipeViewHolder(@NonNull View itemView) {
super(itemView);
mRecipeTextView = (TextView) itemView.findViewById(R.id.recipe_textview);
position = (TextView) itemView.findViewById(R.id.textViewPosition);
total = (TextView) itemView.findViewById(R.id.textViewTotal);
mArrowExpandImageView = (ImageView) itemView.findViewById(R.id.arrow_expand_imageview);
}
public void bind(@NonNull Recipe recipe) {
mRecipeTextView.setText(recipe.getName());
try {
position.setText("Position: " + recipe.getJson().getString("position"));
total.setText("Total Bid Amount: " + recipe.getJson().getString("type"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
The converted Kotlin class is
//Error occurs here in Parentvireholder<*,*>
class RecipeViewHolder(itemView: View) : ParentViewHolder<*, *>(itemView) {
private val mArrowExpandImageView: ImageView
private val mRecipeTextView: TextView
private val position: TextView
private val total: TextView
init {
mRecipeTextView = itemView.findViewById<View>(R.id.recipe_textview) as TextView
position = itemView.findViewById<View>(R.id.textViewPosition) as TextView
total = itemView.findViewById<View>(R.id.textViewTotal) as TextView
mArrowExpandImageView = itemView.findViewById<View>(R.id.arrow_expand_imageview) as ImageView
}
fun bind(recipe: Recipe) {
mRecipeTextView.text = recipe.name
try {
position.text = "Position: " + recipe.json!!.getString("position")
total.text = "Total Bid Amount: " + recipe.json!!.getString("type")
} catch (e: JSONException) {
e.printStackTrace()
}
}
}
Error occurs in shown comment. I.ve tried the Any fix but that shows Type arguments is not within its bounds
Upvotes: 1
Views: 1208
Reputation: 358
Kotlin requires you to specify the generic arguments of the superclass.
You need to replace the *
's in the superclass generic types with an explicit type or specify generic type's for the subclass.
You can't use Any
if the super class generic types are restricted to a particular type. Have a look at the class definition of ParentViewHolder and declare the same types as that does:
RecipeViewHolder<P extends Parent<C>, C>(itemView: View) : ParentViewHolder<P, C>
or
RecipeViewHolder(itemView: View) : ParentViewHolder<MyParent, MyChild>
Upvotes: 1