【问题标题】:Unhandled Exception: MissingPluginException(No implementation found for method ... on channel ...)未处理的异常:MissingPluginException(在通道上找不到方法 ... 的实现 ...)
【发布时间】:2020-03-15 22:27:25
【问题描述】:

非常标准的代码,但不知道为什么我会得到 未处理的异常:MissingPluginException(在通道 com.jetbrains.handson.mpp.mobile/createApplicationScreenMessage 上找不到方法 getHello 的实现)

package com.example.mysharedproject

import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import com.jetbrains.handson.mpp.mobile.createApplicationScreenMessage
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant


class MainActivity : FlutterActivity() {

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "com.jetbrains.handson.mpp.mobile/createApplicationScreenMessage")
            .setMethodCallHandler { call, result ->
                if (call.method == "getHello"){
                    result.success(createApplicationScreenMessage())
                }
                else{
                    result.notImplemented()
                }
            }
    }
}

此代码来自本教程中的 SharedCode https://play.kotlinlang.org/hands-on/Targeting%20iOS%20and%20Android%20with%20Kotlin%20Multiplatform/03_CreatingSharedCode

package com.jetbrains.handson.mpp.mobile

expect fun platformName(): String

fun createApplicationScreenMessage(): String {
    return "Kotlin Rocks on ${platformName()}"
}

和 Dart 代码

class _MyHomePageState extends State<MyHomePage> {
  String _helloText = "...";
  static const MethodChannel methodChannel =
      MethodChannel('com.jetbrains.handson.mpp.mobile/createApplicationScreenMessage');

  Future<void> _getHello() async {
    final String result = await methodChannel.invokeMethod("getHello");
    setState(() {
      _helloText = result;
    });
  }
  @override
  void initState() {
    _getHello();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(_helloText),
          ],
        ),
      ),
    );
  }

我尝试了flutter clean、应用程序重新安装、项目清理和构建,但对我没有任何效果,这个问题的原因可能是什么?

UPD:我不知道我的活动是否应该在 configureFlutterEngine 方法中通过 Logcat 输出此日志,但事实并非如此。 android.util.Log("tagdbg","我在这里")

在 Android Manifest 中,我指定了 &lt;activity android:name=".MainActivity"&gt;,但没有像官方文档中那样指定 FlutterActivity,但我需要从 kotlin/native 访问 kotlin 代码。

【问题讨论】:

    标签: android flutter kotlin-native


    【解决方案1】:

    让我们在你的终端试试这个

    flutter run
    

    【讨论】:

    • 我仍然得到这个异常,现在在终端。检查我的更新,也许它可以帮助找到解决方案。
    【解决方案2】:

    嗯,我已经成功了,但不知道具体是什么解决方案。我可以建议首先使其仅在 Flutter 上工作(使用官方 Flutter 文档中的 MethodChannel),然后尝试使用 kotlin 多平台来实现。我也使用了flutterEngine.dartExecutor而不是flutterEngine.dartExecutor.binaryMessenger

    【讨论】:

      【解决方案3】:

      原因是错过super.configureFlutterEngine(flutterEngine)? 我的代码和你的类似,而且运行良好:

      class MainActivity: FlutterActivity() {
      override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
          super.configureFlutterEngine(flutterEngine)
          GeneratedPluginRegistrant.registerWith(flutterEngine)
          MyMethodChannel.register(this, flutterEngine)
      }
      

      }

      【讨论】:

        【解决方案4】:

        这个异常的原因可能是你的MethodChannel没有注册成功,或者你主动调用了result.notImplemented()

        1. 请在protected void onCreate(@Nullable Bundle savedInstanceState){}里面注册。
        2. 在Java代码的情况下,将call.method == "xxx"替换为call.method.equals("xxx")

        请检查是否调用了onMethodCall,以此来定位问题。

        【讨论】:

        • 你能不能把句子拆成合适的红色和理解,
        猜你喜欢
        • 2020-04-20
        • 2020-11-13
        • 1970-01-01
        • 1970-01-01
        • 2022-07-22
        • 2022-07-24
        • 2021-07-10
        • 2020-05-29
        • 1970-01-01
        相关资源
        最近更新 更多