我正在尝试从Kotlin文件中调用非静态Java方法,因为我需要能够使用findViewById。但是,当我将方法更改为非静态时,Kotlin文件返回以下错误:未解析的引用:main。当Java方法为静态时,Kotlin文件可以运行Java方法。


Kotlin文件:


class RoundFactoryImpl(private val randomiser: Randomiser) : RoundFactory {
     override fun buildRound(cards: List<Card>): Round = Round()
     val card = Round()
     val customer = PokemonRand.main(randomiser, card)
}



Java文件:


public void main(Randomiser randomiser, Round cards) {
    Card cardA, cardB;

    cardA = getCard(randomiser, cards);
    cardB = getCard(randomiser, cards);

    Log.d("cardA", cardA.toString());
    Log.d("cardB", cardB.toString());

    while (cardA.getRarity() == cardB.getRarity()) {
        cardB = getCard(randomiser, cards);
    }

    dispCard(cardA, cardB);
    compCard(cardA, cardB);
}

最佳答案

val customer = PokemonRand.main(randomiser, card)


代码的第一个问题是将main方法的值分配给返回类型为customervoid变量;

代码的第二个问题是您的主要方法不是静态的,因此您需要创建PokemonRand类的实例并在该实例上调用main方法,如下所示

val pokemon = PokemonRand();
pokemon.main(randomiser, card);

08-18 07:24