在学习 Flutter,被一个问题折腾了好几天了,问题简化如下:
Flutter 版本是 v1.17.0;
现有 Android 工程,里面有两个 Activity:
然后又按照官方文档集成了 Flutter module,里面有页面 FlutterRoute,FlutterRoute 中有一个按钮。
现在操作逻辑如下: 点击 MainActivity 中的按钮,跳转到 Flutter 页面,然后点击 FlutterRoute 中的按钮,再跳转到 NativeActivity 。
现在 MainActivity 代码如下:
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity implements View.OnClickListener, MethodChannel.MethodCallHandler {
private Button button;
private static final String CHANNEL_NAME = "TEST_CHANNEL_NAME";
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);
}
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
GeneratedPluginRegistrant.registerWith(flutterEngine);
channel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL_NAME);
channel.setMethodCallHandler(this);
}
@Override
public void onClick(View v) {
startActivity(
FlutterActivity.withNewEngine().build(this)
);
}
@Override
public void onMethodCall(@NonNull MethodCall methodCall, @NonNull MethodChannel.Result result) {
if ("startNativeActivity".equals(methodCall.method)) {
Intent intent = new Intent(MainActivity.this, NativeActivity.class);
startActivity(intent);
}
}
}
Flutter 代码如下:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main(){
runApp(
MaterialApp(
routes: {
"/":(BuildContext context)=>FlutterRoute(),
},
initialRoute: "/",
)
);
}
class FlutterRoute extends StatefulWidget {
FlutterRouteState createState() {
return FlutterRouteState();
}
}
class FlutterRouteState extends State<FlutterRoute> {
static const channel = MethodChannel("TEST_CHANNEL_NAME");
Future<void> startNativeActivity() async{
await channel.invokeMethod("startNativeActivity");
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter 页面"),
),
body: Container(
color: Colors.lightGreenAccent,
child: Center(
child: Column(
children: <Widget>[
Text("Flutter 页面"),
MaterialButton(
color: Colors.redAccent,
child: Text("跳转到原生 Activity"),
onPressed: () {
startNativeActivity();
},
),
],
),
),
),
);
}
}
在 MainActivity 中点击按钮,可以跳转到 FlutterRoute,但是在 FlutterRoute 中点击按钮跳转到 NativeActivity 的时候却抛出:
E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method startNativeActivity on channel TEST_CHANNEL_NAME)
#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 FlutterRouteState.startNativeActivity (package:fluttermodule/main.dart:27:19)
#3 FlutterRouteState.build.<anonymous closure> (package:fluttermodule/main.dart:46:19)
#4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:779:19)
#5 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:862:36)
#6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
#7 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:504:11)
#8 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:282:5)
......
究竟是哪里出了问题啊,折腾好几天了,始终不行,难道 MethodChannel 不是这么用的吗?
百度谷歌官方 githubStackOverflow 搜了个遍。。。
实在没辙了,求点醒求指教,感谢
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.