简体   繁体   English

如何引用 java 活动 class 的公共方法来自 Kotlin 同伴 ZA8CFDE6331BD59EB2AC96F9666

[英]How to refer to a public method of a java Activity class from the Kotlin companion object?

I have a mix of Kotlin and Java code in my Android App.我的 Android 应用程序中混合了 Kotlin 和 Java 代码。 I need to refer to a public void method declared in an Activity having java code from a Kotlin companion object.我需要参考在具有 java 代码的 Activity 中声明的公共 void 方法,该代码来自 Kotlin 伴侣 object。 I tried some ways including making the method static but of no use.我尝试了一些方法,包括制作方法 static 但没有用。

// java code
public class SampleActivity extends Activity{

     public void toggleMark(boolean canTic)
     { // some code }

}

//Kotlin code
class MyUtils {

    companion object {

         fun setTicPref(context: Context)
         {
             // some code 
             (context as SampleActivity).toggleMark(true) // cannot find declaration to go to

         }
    }

}

Make SampleActivity public.公开SampleActivity

change改变

class SampleActivity extends Activity{

     public void toggleMark(boolean canTic)
     { // some code }

}

to

public class SampleActivity extends Activity{

     public void toggleMark(boolean canTic)
     { // some code }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM