如何在android中从java实现Kotlin类?
我是一名Java/Kotlin新手,正在开发android应用程序。我尝试在YourApplication.kt中实现以下内容,我在这里发现: 我补充说如何在android中从java实现Kotlin类?,java,android,android-studio,kotlin,Java,Android,Android Studio,Kotlin,我是一名Java/Kotlin新手,正在开发android应用程序。我尝试在YourApplication.kt中实现以下内容,我在这里发现: 我补充说 ext.kotlin_version = '1.1.51' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 我的毕业剧本。我还将此添加到我的清单中: <activity android:name=".YourApplicat
ext.kotlin_version = '1.1.51'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
我的毕业剧本。我还将此添加到我的清单中:
<activity android:name=".YourApplication"></activity>
但我得到了以下错误:
类“YourApplication”不是抽象的,并且不实现抽象成员公共抽象乐趣onActivityResumed(p0:Activity!)
我按照Android Studio的建议将其抽象化,但这导致了更多的错误
我还尝试按照AS的建议“实现成员”,但这会使我的应用程序在模拟器中崩溃
谢谢大家! 您的内部类,即AppLifecycleTracker类需要重写更多的几个方法
import android.app.Activity
android.app.Application
import android.os.Bundle
/**
* Created by saritha high 13/2/18.
*/
class AirventApplication : Application() {
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(AppLifecycleTracker())
}
class AppLifecycleTracker : Application.ActivityLifecycleCallbacks {
private var numStarted = 0
override fun onActivityStarted(activity: Activity?) {
if (numStarted == 0) {
println("Activity has started");
}
numStarted++
}
override fun onActivityStopped(activity: Activity?) {
numStarted--
if (numStarted == 0) {
println("Activity has stopped");
}
}
override fun onActivityPaused(p0: Activity?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityResumed(p0: Activity?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityDestroyed(p0: Activity?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivitySaveInstanceState(p0: Activity?, p1: Bundle?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityCreated(p0: Activity?, p1: Bundle?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
}
并在清单中声明应用程序类,如:
<application
android:name=".YourApplication"
android:icon="@mipmap/app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".YourActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
您的内部类,即AppLifecycleTracker类,需要重写更多的几个方法
import android.app.Activity
android.app.Application
import android.os.Bundle
/**
* Created by saritha high 13/2/18.
*/
class AirventApplication : Application() {
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(AppLifecycleTracker())
}
class AppLifecycleTracker : Application.ActivityLifecycleCallbacks {
private var numStarted = 0
override fun onActivityStarted(activity: Activity?) {
if (numStarted == 0) {
println("Activity has started");
}
numStarted++
}
override fun onActivityStopped(activity: Activity?) {
numStarted--
if (numStarted == 0) {
println("Activity has stopped");
}
}
override fun onActivityPaused(p0: Activity?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityResumed(p0: Activity?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityDestroyed(p0: Activity?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivitySaveInstanceState(p0: Activity?, p1: Bundle?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onActivityCreated(p0: Activity?, p1: Bundle?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
}
并在清单中声明应用程序类,如:
<application
android:name=".YourApplication"
android:icon="@mipmap/app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".YourActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
您创建了应用程序类,但尝试将其作为活动。这是错误的。您到底需要什么,活动类还是应用程序类?您创建了应用程序类,但尝试将其作为活动。这是错误的。你到底需要什么,活动类还是应用类?谢谢你的回复,但我已经尝试过了(Android Studio建议),但它只会导致我的应用程序崩溃。不会向用户显示其他错误console@Rufus删除所有TODO函数调用,它将停止崩溃感谢回复,但我已经尝试过了(Android Studio建议),但它只会导致我的应用程序崩溃。不会向用户显示其他错误console@Rufus删除所有TODO函数调用,它将停止崩溃