简体   繁体   English

如何从Android中的Java实现Kotlin类?

[英]How to implement Kotlin class from java in android?

I'm a Java/Kotlin newbie, working on an android app. 我是Java / Kotlin新手,正在开发Android应用程序。 I tried to implement the following into YourApplication.kt, that I found here: https://stackoverflow.com/a/42679191/4666306 我试图将以下内容实现到YourApplication.kt中,在这里找到: https ://stackoverflow.com/a/42679191/4666306

package com.tijaname.fortysix

import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.util.Log.println

class YourApplication : 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");
        }
    }

}

I added 我加了

 ext.kotlin_version = '1.1.51'
 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

to my gradle scripts. 到我的gradle脚本。 I also added this to my manifest: 我还将此添加到清单中:

         <activity android:name=".YourApplication"></activity>

I tried to invoke the kotlin script like this (from my MainActivity in java): 我试图这样调用kotlin脚本(从我在Java中的MainActivity):

 Intent intent = new Intent(getBaseContext(), YourApplication.class);
    startActivity(intent);

But I get the following error: 但是我收到以下错误:

Class 'YourApplication' is not abstract and does not implement abstract member public abstract fun onActivityResumed(p0: Activity!)... 类'YourApplication'不是抽象的,并且没有实现抽象成员公共抽象乐趣onActivityResumed(p0:Activity!)...

I followed Android-Studio's suggestions to make it abstract, but that led to more errors. 我遵循Android-Studio的建议使其抽象,但这导致了更多错误。

I also tried to 'Implement members' per AS's suggestion, but this makes my app crash in the emulator. 我还尝试按照AS的建议“实施成员”,但这会使我的应用在模拟器中崩溃。

Thank you! 谢谢!

Your inner class ie, AppLifecycleTracker class need to override few more methods. 您的内部类(即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.
      }
 }
}

And declare your application class in Manifest like: 并在清单中声明您的应用程序类,例如:

  <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM