kotlin自定view構(gòu)造方法寫法

  • 正確寫法
  • 錯(cuò)誤寫法

一、正確寫法:

  • 方法1:@JvmOverloads
class KoTabBottom @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {
    init { }
}

對(duì)上面代碼進(jìn)行kotlin轉(zhuǎn)java如下:

public final class KoTabBottom extends RelativeLayout {
   @JvmOverloads
   public KoTabBottom (@NotNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
      Intrinsics.checkNotNullParameter(context, "context");
      super(context, attrs, defStyleAttr);
   }

   // $FF: synthetic method
   public KoTabBottomLayout(Context var1, AttributeSet var2, int var3, int var4, DefaultConstructorMarker var5) {
      if ((var4 & 2) != 0) {
         var2 = (AttributeSet)null;
      }

      if ((var4 & 4) != 0) {
         var3 = 0;
      }

      this(var1, var2, var3);
   }

   @JvmOverloads
   public KoTabBottomLayout(@NotNull Context context, @Nullable AttributeSet attrs) {
      this(context, attrs, 0, 4, (DefaultConstructorMarker)null);
   }

   @JvmOverloads
   public KoTabBottomLayout(@NotNull Context context) {
      this(context, (AttributeSet)null, 0, 6, (DefaultConstructorMarker)null);
   }
   ........
}

注意:這里有一個(gè)KoTabBottomLayout(@NotNull Context context)的構(gòu)造方法,下面的錯(cuò)誤寫法中沒(méi)有此構(gòu)造方法。

  • 方法2:
class KoTabBottom : RelativeLayout {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int)
            : super(context, attrs, defStyleAttr)
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int)
            : super(context, attrs, defStyleAttr, defStyleRes)

    init { }
}

二、 錯(cuò)誤寫法

class KoTabBottom(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {
    init { }
}

注意:這種寫法會(huì)導(dǎo)致java.lang.NoSuchMethodException錯(cuò)誤,原因是在xml中創(chuàng)建控件需要一個(gè)context的構(gòu)造函數(shù)。

對(duì)上面代碼進(jìn)行kotlin轉(zhuǎn)java如下:

public final class KoTabBottom extends RelativeLayout {
   ........
   public KoTabBottom(@NotNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
      Intrinsics.checkNotNullParameter(context, "context");
      super(context, attrs, defStyleAttr);
   }
   ........
   // $FF: synthetic method
   public KoTabBottom(Context var1, AttributeSet var2, int var3, int var4, DefaultConstructorMarker var5) {
      if ((var4 & 2) != 0) {
         var2 = (AttributeSet)null;
      }

      if ((var4 & 4) != 0) {
         var3 = 0;
      }

      this(var1, var2, var3);
   }
   ........
}

錯(cuò)誤分析:kotlin轉(zhuǎn)換后的java代碼是沒(méi)有context的構(gòu)造方法的。所以錯(cuò)誤日志顯示信息為java.lang.NoSuchMethodException

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容