类型不匹配: 推断类型为MainActivity但需要的是FlutterEngine

4

我是Flutter的新手,想要更改设备的壁纸,但这需要通过平台通道进行方法调用。 https://developer.android.com/reference/android/Manifest.permission?hl=en#SET_WALLPAPER本机安卓wallpaperManager

在android文件夹下的MainActivity.kt Kotlin文件中会抛出错误。

我尝试了以下操作:

  • 使用MainActivity.java文件创建一个新项目
  • 运行flutter clean
  • 运行flutter pub cache repair

我从这里阅读了其他类型不匹配的错误问题,但不幸的是没有找到解决办法。

任何帮助都将不胜感激。 我的代码如下所示。

MainActivity.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
}
}

主文件.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稳定版


大家好,我通过更改方法调用方式解决了这个问题,因为Flutter进行了更新。(: - Ihsan ALGUL
FlutterView已被弃用,因此我使用flutterEngine和Java而不是Kotlin。 - Ihsan ALGUL
3个回答

6
请从MainActivity.kt中删除GeneratedPluginRegistrant.registerWith(flutterEngine);,并按如下更新。
import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}

2

要传递Flutter引擎值,我们可以使用provideFlutterEngine方法。对于flutterView,我们可以使用flutterEngine.dartExecutor。以下是代码片段。

class MainActivity: FlutterActivity() {
private val CHANNEL = "com.startActivity/testChannel"
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    provideFlutterEngine(this)?.let { GeneratedPluginRegistrant.registerWith(it) }
    MethodChannel(flutterEngine?.dartExecutor,CHANNEL).setMethodCallHandler{ call, result ->
        if(call.method.equals("StartSecondActivity")){
            val intent= Intent(this,KotlinActivity::class.java)
            startActivity(intent)
            result.success("ActivityStarted")
        }
        else{
            result.notImplemented()
        }
    }
}}

0

您可以使用flutter create或通过IDE向导创建新应用程序,MainActivity.kt可能如下所示:

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}

然后从它开始工作。


谢谢您的回复,我尝试了但结果与上述提到的一样失败了。 - Ihsan ALGUL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接