[英]How to move from one activity to another i.e Java Class to Kotlin Class in an Android Java Project?
I am using the following ways but in all the cases in the first class which is Java Class, there is an error during run time.我正在使用以下方法,但在第一个 class 的所有情况下,即 Java Class 中,运行时出现错误。 Here LoginActivity is the Java class and VerifyMobile Activity is the Kotlin class.
这里 LoginActivity 是 Java class 和 VerifyMobile Activity 是 Kotlin ZA2F2ED4F8EBC2CBB4C21A2DC9。
Intent intent = new Intent(this, VerifyMobile.class);
(Run Time Error) Intent intent = new Intent(LoginActivity.this, VerifyMobile.class);
(Run Time Error) Intent intent = new Intent(this, VerifyMobile::class.java);
(Compiler Error) Intent intent = new Intent(LoginActivity.this, VerifyMobile::class.java);
(Compiler Error) Looking for the solution.寻找解决方案。
In 3 and 4, it looks like Kotlin and Java syntax is being mixed together.在 3 和 4 中,看起来 Kotlin 和 Java 语法混合在一起。
To create an intent in a Java file ( .java
), do this:要在 Java 文件 (
.java
) 中创建意图,请执行以下操作:
Intent intent = new Intent(context, VerifyMobile.class);
To create an intent in a Kotlin file ( .kt
), do this:要在 Kotlin 文件 (
.kt
) 中创建意图,请执行以下操作:
val intent = Intent(context, VerifyMobile::class.java)
It doesn't matter what language the Activity
being navigated to is written in. What matters is the language of the file the code is being written in.被导航到的
Activity
是用什么语言编写的并不重要。重要的是编写代码的文件的语言。
To init another activity you should create your intent with the current activity to the next one like this:要启动另一个活动,您应该使用当前活动创建下一个活动的意图,如下所示:
Java: Java:
Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);
Kotlin: Kotlin:
val intent = Intent(this, AnotherActivity::class.java)
startActivity(intent)
or:或者:
Java Java
startActivity(new Intent(this, AnotherActivity.class));
Kotlin: Kotlin:
startActivity(Intent(this, AnotherActivity::class.java))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.