非常标准的代码,但是不知道为什么 未处理的异常:MissingPluginException(在com.jetbrains.handson.mpp.mobile/createApplicationScreenMessage通道上未找到方法getHello的实现)
SomeBlock::new
此代码来自本教程的SharedCode https://play.kotlinlang.org/hands-on/Targeting%20iOS%20and%20Android%20with%20Kotlin%20Multiplatform/03_CreatingSharedCode:
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()
}
}
}
}
和Dart代码
package com.jetbrains.handson.mpp.mobile
expect fun platformName(): String
fun createApplicationScreenMessage(): String {
return "Kotlin Rocks on ${platformName()}"
}
我尝试过flutter clean,重新安装应用程序,项目clean&build,但对我没有任何帮助,此问题的原因可能是什么?
UPD::我不知道我的活动是否应该通过configureFlutterEngine方法中的 Logcat 输出此日志,但不是。 android.util.Log(“ tagdbg”,“我在这里”)
在Android Manifest中,我指定了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),
],
),
),
);
}
,但没有像官方文档中那样指定FlutterActivity,但是我需要从kotlin / native中访问kotlin代码。
答案 0 :(得分:0)
让我们在您的终端中尝试一下
flutter run
答案 1 :(得分:0)
好吧,我已经使它起作用了,但是不知道到底有什么解决方案有效。我可以建议首先使它仅在Flutter上起作用(使用官方flutter文档中的MethodChannel),然后尝试使用kotlin multiplatform来实现。我也用flutterEngine.dartExecutor代替flutterEngine.dartExecutor.binaryMessenger
答案 2 :(得分:0)
原因是错过了super.configureFlutterEngine(flutterEngine)? 我的代码与您的代码相似,并且可以正常运行:
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
GeneratedPluginRegistrant.registerWith(flutterEngine)
MyMethodChannel.register(this, flutterEngine)
}
}
答案 3 :(得分:0)
出现这个异常的原因可能是你的MethodChannel没有注册成功,或者你主动调用了result.notImplemented(),
protected void onCreate(@Nullable Bundle savedInstanceState){}
内注册。call.method == "xxx"
替换为 call.method.equals("xxx")
。请检查是否调用了onMethodCall,以此来定位问题。