.java は 2 または 3 引数の View コンストラクタを使用していません。XML 属性は機能しません。質問する

.java は 2 または 3 引数の View コンストラクタを使用していません。XML 属性は機能しません。質問する

私は初心者で、ゲームを作るのに問題があります

プロセスを実行する

activity_main.xml -> MainActivity.java -> GameLoop.java -> action.xml (エラー) -> CustomView.java

Custom view CustomView is not using the 2- or 3-argument View constructors; XML attributes will not work

理解できない......

ベストアンサー1

CustomView の View の他の 2 つのコンストラクターをオーバーライドする必要があります。

public CustomView(Context context) {
    super(context);
    init(context);
}

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context context) {
    //do stuff that was in your original constructor...
}

おすすめ記事