Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start kotlin activity *.kt from java activity *.java?

Invalidate Caches/ restart... helps me!

My code in java class is:

Intent intent = new Intent(view.getActivity(), AddPaymentActivity.class);
view.getActivity().startActivity(intent);

AddPaymentActivity has kotlin extention .kt
Got error java.lang.NoClassDefFoundError

like image 215
Yvgen Avatar asked Aug 10 '16 15:08

Yvgen


People also ask

Can you call Kotlin code from Java?

One of the great wonders of Kotlin is that it's fully integrated with Java. This means that although all your application code is written Java, you can create a class in Kotlin and use it from Java without any issues. Calling Kotlin from Java code can't be easier.

Can you mix Kotlin and Java in the same project?

Even if it's interop you can't mix Java and Kotlin in the same file. If you really want to have static methods/variables you can use an companion object . You can also access create a "real" static method in your JVM by using @JvmStatic . By using @JvmStatic you can use Java to access your Static methods like before.

How do I intent to start another activity?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.


2 Answers

File -> Invalidate Caches/ restart...

like image 76
Yvgen Avatar answered Sep 16 '22 15:09

Yvgen


In my case, I forgot to add

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

and

apply plugin: 'kotlin-android'
...
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

to build.gradle

Example

project build.gradle

...
buildscript {
  ext.kotlin_version = '1.1.51'
  dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    ...
  }
}

allprojects {
  repositories {
    jcenter()
    google()
  }
}

app build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
...

dependencies {
  ...
  compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
like image 40
Linh Avatar answered Sep 20 '22 15:09

Linh