Reputation:
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.
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):
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? 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 don't know if I should also do this in the kotlin class I am referencing:
@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)
Upvotes: 0
Views: 568
Reputation: 154
What do you mean by "invoke a kotlin class"? If you mean to reference it, then simply do Chat_Bot_Main.class
. Basically, treat it as if it's a regular 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):
src
└───main
├───java
│ └─── <java packages>
├───kotlin
│ └─── <kotlin packages>
└───resources
Upvotes: 1