如何在android中实现Java中的Kotlin类?

时间:2018-02-23 06:56:18

标签: java android android-studio kotlin

我是一名Java / Kotlin新手,正在开发一款Android应用程序。我尝试将以下内容实现到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");
        }
    }

}

我添加了

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

到我的gradle脚本。我还将此添加到我的清单中:

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

我试图像这样调用kotlin脚本(来自我在java中的MainActivity):

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

但是我收到以下错误:

Class&#39; YourApplication&#39;是不抽象的,并没有实现抽象成员公共抽象乐趣onActivityResumed(p0:Activity!)......

我按照Android-Studio的建议将其抽象化,但这导致了更多错误。

我还尝试过“实施会员”。根据AS的建议,但这会让我的应用程序在模拟器中崩溃。

谢谢!

1 个答案:

答案 0 :(得分:1)

你的内部类,即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.
      }
 }
}

并在Manifest中声明您的应用程序类,如:

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