简体   繁体   English

How to move from one activity to another ie Java Class to Kotlin Class in an Android Java Project?

[英]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。

  1. Intent intent = new Intent(this, VerifyMobile.class); (Run Time Error) (运行时错误)
  2. Intent intent = new Intent(LoginActivity.this, VerifyMobile.class); (Run Time Error) (运行时错误)
  3. Intent intent = new Intent(this, VerifyMobile::class.java); (Compiler Error) (编译器错误)
  4. Intent intent = new Intent(LoginActivity.this, VerifyMobile::class.java); (Compiler Error) (编译器错误)

Looking for the solution.寻找解决方案。

  1. Make the changes in the first class which is Java class在第一个 class 中进行更改,即 Java class
  2. Make the changes in the second class which is Kotlin Class.在第二个 class 中进行更改,即 Kotlin Class。

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.

相关问题 如何将变量的值从jsf的bean页面(即bean.java)移动到另一个Java类? - how to move a variable's value from jsf's bean page (i.e, bean.java) to another java class? 如何在同一项目中从一个 Java 活动移动到另一个 Kotlin 活动? - How do I move from one Java Activity to another Kotlin Activity in same project? 如何将 android 活动中的数据传递给 Java class? - How to pass data from one android activity to an Java class? 如何从Android中的Java实现Kotlin类? - How to implement Kotlin class from java in android? 如何将一种方法从一个 class 迁移到另一个? (安卓、Java) - How to migrate one method from one class to another? (Android, Java) 从 java 活动中调用 kotlin class - call a kotlin class from a java activity Android Java:设置从一个活动发送到另一个 Java 类的参数时出错 - Android Java: Error in setting parameter sent from one activity to another java class 如何从Android Activity调用Java类 - How to call a Java class from a Android Activity 如何在Java中动态运行时将.class文件从一个项目(项目2)加载到另一个Web项目(项目3)? - How can I load .class files from one project(project 2) to another web project(Project 3) at dynamically runtime in java? 如何在Android Studio中从Java类转换为Kotlin类 - How to intent from a java class to a kotlin class in Android Studios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM