簡體   English   中英

未處理的異常:MissingPluginException(在頻道上找不到方法......的實現......)

[英]Unhandled Exception: MissingPluginException(No implementation found for method ... on channel ...)

非常標准的代碼,但不知道為什么會出現未處理的異常: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()}"
}

和飛鏢代碼

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 中,我指定了<activity android:name=".MainActivity">而不是 FlutterActivity,如官方文檔中那樣,但我需要從 kotlin/native 訪問 kotlin 代碼。

讓我們在你的終端中試試這個

flutter run

好吧,我已經讓它工作了,但不知道究竟是什么解決方案影響了。 我可以建議首先使其僅在 Flutter 上工作(使用來自官方 Flutter 文檔的 MethodChannel),然后嘗試使用 kotlin 多平台來實現。 我還使用了 flutterEngine.dartExecutor 而不是 flutterEngine.dartExecutor.binaryMessenger

原因是miss super.configureFlutterEngine(flutterEngine)? 我的代碼和你的差不多,運行正常:

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

}

這個異常的原因可能是你的MethodChannel沒有注冊成功,或者你主動調用了result.notImplemented()

  1. 請在protected void onCreate(@Nullable Bundle savedInstanceState){}內注冊。
  2. 對於 Java 代碼,將call.method == "xxx"替換為call.method.equals("xxx")

請檢查是否onMethodCall被調用時,使用此定位probleml。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM