简体   繁体   English

从 java 活动中调用 kotlin class

[英]call a kotlin class from a java activity

I want to create an intent to a button from a java class in which when clicked the user will be redirected to a kotlin class.我想从 java class 创建一个按钮的意图,单击该按钮时,用户将被重定向到 Z4FBA3BC02B72EE9A687A1E5286E373C6ABB4A2DZ2A29FEBDC4Z ZABB4A2DZ2A29FEBDC4Z

I am trying these lines, but the Chat_Box_Main.kt is showing error saying that it cannot find it (It is in the same directory as the java class I am typing these lines):我正在尝试这些行,但 Chat_Box_Main.kt 显示错误,说它找不到它(它与 java class 在同一目录中,我正在输入这些行):

public void  openChatBot() {
    Intent intent = new Intent(this, Chat_Bot_Main.kt);
    startActivity(intent);
}

Do you have any tips on how to properly invoke a kotlin class from a java class?您对如何从 java ZA2F2ED4F8EBC2CBB4C21A29 It would be a big help.这将是一个很大的帮助。

Additional errors:附加错误:

I have these lines in my home_page java class where I will reference the kotlin class through a button when it is clicked (id.Chatbot). I have these lines in my home_page java class where I will reference the kotlin class through a button when it is clicked (id.Chatbot). I don't know if I should also do this in the kotlin class I am referencing:我不知道我是否也应该在 kotlin class 我引用:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_page);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_baseline_arrow_back_ios_24);
        uiThread = Thread.currentThread();
        handler = new Handler();
        textView = findViewById(R.id.text_View_Chatbot_Main); // Chat_Bot_Main is a textfield in the kotlin class
        new Thread() {
            @Override
            public void run() {
                Button chatbot = findViewById(R.id.Chatbot); // Chatbot is a button on Home_Page.xml
                chatbot.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      //  openChatBot();
                        updateUI();
                    }
                });
            }
        }.start();


public void updateUI() {
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            textView.setText("Chatbot Thread");
        }
    };
    runOnUi(runnable);
}

private void runOnUi(Runnable runnable) {
    if (Thread.currentThread() == uiThread) {
        runnable.run();
    } else {
        handler.post(runnable);
    }
}

This is the error that I get when I debug it:这是我调试它时遇到的错误:

 2021-05-14 01:34:02.559 32313-32313/com.example.astrocare E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.astrocare, PID: 32313
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.example.astrocare.otherclasses.Home_Page$6.run(Home_Page.java:85)
    at com.example.astrocare.otherclasses.Home_Page.runOnUi(Home_Page.java:93)
    at com.example.astrocare.otherclasses.Home_Page.updateUI(Home_Page.java:88)
    at com.example.astrocare.otherclasses.Home_Page$1$1.onClick(Home_Page.java:38)
    at android.view.View.performClick(View.java:7448)
    at android.view.View.performClickInternal(View.java:7425)
    at android.view.View.access$3600(View.java:810)
    at android.view.View$PerformClick.run(View.java:28305)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

What do you mean by "invoke a kotlin class"? “调用 kotlin 类”是什么意思? If you mean to reference it, then simply do Chat_Bot_Main.class .如果您想引用它,那么只需执行Chat_Bot_Main.class Basically, treat it as if it's a regular Java class.基本上,将其视为普通的 Java class。

Note that the directory structure changes when you add Kotlin to a project (assuming the class is yours, and not from a dependency):请注意,当您将 Kotlin 添加到项目时,目录结构会发生变化(假设 class 是您的,而不是依赖项):

src
└───main
    ├───java
    │   └─── <java packages>
    ├───kotlin
    │   └─── <kotlin packages>
    └───resources

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

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