Android Project добавляет модуль Flutter, выбрасывает MissingPluginException
У меня есть проект для Android, и я добавляю в него модуль flutter. MainActivity проекта Android следующим образом:
public class MainActivity extends FlutterActivity implements View.OnClickListener, MethodChannel.MethodCallHandler {
private Button button;
private MethodChannel channel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(this);
GeneratedPluginRegistrant.registerWith(getFlutterEngine());
channel = new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(),CHANNEL_NAME);
channel.setMethodCallHandler(this);
}
@Override
public void onClick(View v) {
startActivity(
FlutterActivity.withNewEngine().build(this)
);
}
private static final String CHANNEL_NAME = "com.lixyz.androidwithflutter";
@Override
public void onMethodCall(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
if ("startNativeActivity".equals(methodCall.method)) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
result.success("");
}
}
}
В модуле Flutter у меня есть кнопка, нажмите эту кнопку, запустите SecondActivity проекта Android. Вот такой код флаттера:
class FlutterRoute extends StatefulWidget {
FlutterRouteState createState() {
return SecondRouteState();
}
}
class FlutterRouteState extends State<FlutterRoute> {
static const channel = MethodChannel("com.lixyz.androidwithflutter");
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(""),
),
body: Container(
color: Colors.lightGreenAccent,
child: Center(
child: Column(
children: <Widget>[
MaterialButton(
color: Colors.redAccent,
child: Text("to native Activity"),
onPressed: () {
channel.invokeListMethod("startNativeActivity");
},
),
],
),
),
),
);
}
}
но, щелкнув кнопку Flutter'а, выбросьте исключение MissingPluginException, например:
E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method startNativeActivity on channel com.lixyz.androidwithflutter)
#0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:154:7)
<asynchronous suspension>
#1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
#2 MethodChannel.invokeListMethod (package:flutter/src/services/platform_channel.dart:342:40)
#3 FlutterRouteState.build.<anonymous closure> (package:fluttermodule/FlutterRoute.dart:31:27)
......
Мой Flutter vierson: v1.17.0; что случилось, как мне это исправить? благодарю вас!