簡體   English   中英

從 java 活動中調用 kotlin class

[英]call a kotlin class from a java activity

我想從 java class 創建一個按鈕的意圖,單擊該按鈕時,用戶將被重定向到 Z4FBA3BC02B72EE9A687A1E5286E373C6ABB4A2DZ2A29FEBDC4Z ZABB4A2DZ2A29FEBDC4Z

我正在嘗試這些行,但 Chat_Box_Main.kt 顯示錯誤,說它找不到它(它與 java class 在同一目錄中,我正在輸入這些行):

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

您對如何從 java ZA2F2ED4F8EBC2CBB4C21A29 這將是一個很大的幫助。

附加錯誤:

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). 我不知道我是否也應該在 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);
    }
}

這是我調試它時遇到的錯誤:

 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)

“調用 kotlin 類”是什么意思? 如果您想引用它,那么只需執行Chat_Bot_Main.class 基本上,將其視為普通的 Java class。

請注意,當您將 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