简体   繁体   English

Flutter 'onCreate' 不会覆盖任何内容

[英]Flutter 'onCreate' overrides nothing

I want to add some kotlin code on my flutter app.我想在我的 flutter 应用程序上添加一些 kotlin 代码。 I add onCreate function on project_name/android/app/src/main/kotlin/com/example/app/MainActivity.kt .我在project_name/android/app/src/main/kotlin/com/example/app/MainActivity.kt添加 onCreate function 。

This is my full MainActivity.kt这是我完整的MainActivity.kt

import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import android.os.Environment
import android.net.Uri

import android.content.Intent

class MainActivity: FlutterActivity() {
    private val channel = "externalStorage";
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel).setMethodCallHandler { call, result ->
            when (call.method) {
                "getExternalStorageDirectory" ->
                    result.success(Environment.getExternalStorageDirectory().toString())
                "getExternalStoragePublicDirectory" -> {
                    val type = call.argument<String>("type")
                    result.success(Environment.getExternalStoragePublicDirectory(type).toString())
                }
                else -> result.notImplemented()
            }
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!Settings.canDrawOverlays(getApplicationContext())) {
            val myIntent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
            val uri: Uri = Uri.fromParts("package", getPackageName(), null)
            myIntent.setData(uri)
            startActivityForResult(myIntent, REQUEST_OVERLAY_PERMISSIONS)
            return
        }
    }
}

then I got this error on my build然后我在构建时遇到了这个错误

'onCreate' overrides nothing
Unresolved reference: Settings
Unresolved reference: Settings
Unresolved reference:
REQUEST_OVERLAY_PERMISSIONS

Do I need to import some code?我需要导入一些代码吗?

Finally, I found the answer.最后,我找到了答案。 I need to import some code to add onCreate method.我需要导入一些代码来添加onCreate方法。

  1. import android.os.Bundle for Bundle parameter.Bundle参数import android.os.Bundle See this link.请参阅链接。
  2. import android.provider.Settings because I use Settings.ACTION_MANAGE_OVERLAY_PERMISSION . import android.provider.Settings因为我使用Settings.ACTION_MANAGE_OVERLAY_PERMISSION See this link.请参阅链接。
  3. I forgot to add var REQUEST_OVERLAY_PERMISSIONS = 100我忘了添加var REQUEST_OVERLAY_PERMISSIONS = 100

And now it works.现在它可以工作了。

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

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