1

我有一个 android 项目,然后我在这个项目中添加了一个颤振模块。android项目的MainActivity是这样的:

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 模块中,我有一个按钮,按下这个按钮,启动 android 项目的 SecondActivity。颤振代码如下:

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)
     ......

我的颤振 vierson : v1.17.0 ; 发生了什么事,我该如何解决?谢谢你!

4

0 回答 0