【发布时间】: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 中,我指定了 <activity android:name=".MainActivity">,但没有像官方文档中那样指定 FlutterActivity,但我需要从 kotlin/native 访问 kotlin 代码。
【问题讨论】:
标签: android flutter kotlin-native