android - 类型不匹配 : inferred type is MainActivity but FlutterEngine was expected

标签 android flutter kotlin type-mismatch flutter-method-channel

我是 flutter 的新手,我想更改设备的墙纸,但这需要从平台 channel 调用方法 https://developer.android.com/reference/android/Manifest.permission?hl=en#SET_WALLPAPERnative android wallpaperManager

android文件夹/MainActivity.kt下的Kotlin文件报错。

我在下面尝试了这些:

  • 一个带有 MainActivity.java 文件的新项目
  • flutter 干净
  • flutter pub 缓存修复

我从这里阅读了其他类型不匹配错误问题,但不幸的是没有找到任何东西。

任何帮助都适用。 我的代码如下。

主要 Activity .kt

    package com.combasis.wallpaper_son_app
import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import java.io.IOException
import android.app.WallpaperManager
import android.graphics.BitmapFactory
import java.io.File
import android.os.Build
import android.annotation.TargetApi
import android.content.Context
import io.flutter.Log

private const val CHANNEL = "com.combasis.wallpaper_son_app"
class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        GeneratedPluginRegistrant.registerWith(this)

    MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
        if (call.method == "setWallpaper") {
            val arguments = call.arguments as ArrayList<*>
            val setWallpaper = setWallpaper(arguments[0] as String, applicationContext, arguments[1] as Int)

            if (setWallpaper == 0) {
                result.success(setWallpaper)
            } else {
                result.error("UNAVAILABLE", "", null)
            }
        } else {
            result.notImplemented()
        }
    }
}

@TargetApi(Build.VERSION_CODES.ECLAIR)
private fun setWallpaper(path: String, applicationContext: Context, wallpaperType: Int): Int {
    var setWallpaper = 1
    val bitmap = BitmapFactory.decodeFile(path)
    val wm: WallpaperManager? = WallpaperManager.getInstance(applicationContext)
    setWallpaper = try {
        wm?.setBitmap(bitmap, null, true, wallpaperType)
        0
    } catch (e: IOException) {
        1
    }

    return setWallpaper
}
}

main.dart:...

  static const platform = const MethodChannel('com.combasis.wallpaper_son_app');
  Future<void> _setWallpaper(int wallpaperType, String url) async {
    var file =
    await DefaultCacheManager().getSingleFile(url);
    try {
      final int result = await platform
          .invokeMethod('setWallpaper', [file.path, wallpaperType]);
      print('Wallpaper Updated.... $result');
    } on PlatformException catch (e) {
      print("Failed to Set Wallpaper: '${e.message}'.");
    }
  }

运行:

 Launching lib/main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...
e: /Users/username/AndroidStudioProjects/walpaperz_app/wallpaper_son_app/android/app/src/main/kotlin/com/combasis/wallpaper_son_app/MainActivity.kt: (21, 48): Type mismatch: inferred type is MainActivity but FlutterEngine was expected

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 13s
Exception: Gradle task assembleDebug failed with exit code 1

-Android Studio版本:4.0.1

-Kotlin版本:1.3.72-release-Studio4.0-5稳定

-Flutter版本:1.17.2稳定

最佳答案

请从 MainActivity.kt 中移除 GeneratedPluginRegistrant.registerWith(flutterEngine); 并更新如下。

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}

关于android - 类型不匹配 : inferred type is MainActivity but FlutterEngine was expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63349813/

相关文章:

android - 在 Android Studio 中渲染时不支持 major.minor 版本 52.0

flutter - 当 showMenu 方法在 flutter 中调用时,如何不关闭键盘?

flutter - 如何解决 “Expected a value of type ' int',但在Flutter中得到了 'String'类型之一

kotlin - 如何在 Dokka 中制作嵌套列表?

android - 使用 Kotlin 创建新目录,Mkdir() 不起作用

android - Camera Intent 创建空图片

java - 应用程序不会创建新目录

android - Flutter.dev 和 macOS Catalina (Gatekeeper) 的问题

javascript - Kotlinx-html ref 事件

java - 链接 Activity 会导致 OOM