我一直在与最近的Kotlin一起玩,并且它是真棒!如何使Kotlin停止铸造参数到错误的类(界面)
我正在与Twitter4j库一起尝试Twitter API的一些东西。我在科特林
object Demo {
private val twitterStream = TwitterStreamFactory().instance
@JvmStatic
fun main(args: Array<String>) {
val listener = object : StatusListener {
override fun onStallWarning(warning: StallWarning?) {
println("Got stall warning:" + warning)
}
override fun onScrubGeo(userId: Long, upToStatusId: Long) {
println("Got scrub_geo event userId:$userId upToStatusId:$upToStatusId")
}
override fun onStatus(status: Status) {
println("@" + status.user.screenName + " - " + status.text)
}
override fun onDeletionNotice(statusDeletionNotice: StatusDeletionNotice) {
println("Got a status deletion notice id:" + statusDeletionNotice.statusId)
}
override fun onTrackLimitationNotice(numberOfLimitedStatuses: Int) {
println("Got track limitation notice:" + numberOfLimitedStatuses)
}
override fun onException(ex: Exception) {
ex.printStackTrace()
}
}
twitterStream.addListener(listener)
twitterStream.sample()
}
}
写了这个代码,但每次我遇到它的时候,我得到了exception in thread "main" java.lang.IllegalAccessError: tried to access class twitter4j.StreamListener from class co.enoobong.eno.twitter.bot.Demo at co.enoobong.eno.twitter.bot.Demo.main(Demo.kt:63)
经进一步调查,工具 - > Kotlin->显示科特林字节码。我反编译为Java,只是发现这是正在生成的内容。
@JvmStatic
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
<undefinedtype> listener = new StatusListener() {
public void onStallWarning(@Nullable StallWarning warning) {
String var2 = "Got stall warning:" + warning;
System.out.println(var2);
}
public void onScrubGeo(long userId, long upToStatusId) {
String var5 = "Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId;
System.out.println(var5);
}
public void onStatus(@NotNull Status status) {
Intrinsics.checkParameterIsNotNull(status, "status");
String var2 = "@" + status.getUser().getScreenName() + " - " + status.getText();
System.out.println(var2);
}
public void onDeletionNotice(@NotNull StatusDeletionNotice statusDeletionNotice) {
Intrinsics.checkParameterIsNotNull(statusDeletionNotice, "statusDeletionNotice");
String var2 = "Got a status deletion notice id:" + statusDeletionNotice.getStatusId();
System.out.println(var2);
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
String var2 = "Got track limitation notice:" + numberOfLimitedStatuses;
System.out.println(var2);
}
public void onException(@NotNull Exception ex) {
Intrinsics.checkParameterIsNotNull(ex, "ex");
ex.printStackTrace();
}
};
twitterStream.addListener((StreamListener)listener);
twitterStream.sample();
}
科特林试图投监听到StreamListener
这是私人图书馆(StatusListener其扩展)因此IllegalAccessError
任何想法如何解决好吗?
PS:这里是代码的工作Java版本 - https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/stream/PrintSampleStream.java
PPS:我使用的是1.1.4科特林上的IntelliJ IDEA 2017年2月2日
感谢您的回复@Kiskae。我会试试这个。实际上,它在Java中完美工作,所以不需要修复程序 – Enoobong
也可以像魅力一样工作。非常感谢 – Enoobong