Flutter:MainActivity 不能在 Java Native 代码中转换为 FlutterEngine
2022-09-03 07:58:05
我正在尝试在Java中为Flutter创建一个插件。我正在尝试在我的主类中传递参数。Android activity
Java
这是我的原生代码:
package com.example.trsurveys;
import androidx.annotation.NonNull;
import android.app.Activity;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import theoremreach.com.theoremreach.TheoremReach;
public class TrsurveysPlugin implements FlutterPlugin, MethodCallHandler {
private MethodChannel channel;
private Registrar registrar;
private Activity activity;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "trsurveys");
channel.setMethodCallHandler(this);
}
When I try to debug my app it shows me the following errors
private TrsurveysPlugin(Activity activity) {
this.activity = activity;
}
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "trsurveys");
channel.setMethodCallHandler(new TrsurveysPlugin(registrar.activity()));
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("init")) {
String apiKey = call.argument("apiKey");
TheoremReach.initWithApiKeyAndUserIdAndActivityContext(apiKey, "USER_ID", activity);
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
当我尝试调试我的应用程序时,我收到以下错误:
error: constructor TrsurveysPlugin in class TrsurveysPlugin cannot be applied to given types;
flutterEngine.getPlugins().add(new com.example.trsurveys.TrsurveysPlugin());
^
required: Activity
found: no arguments
reason: actual and formal argument lists differ in length
...path/Downloads/TheoremReach-error/example/android/app/src/main/java/com/example/trsurveys_example/MainActivity.java:11: error: incompatible types: MainActivity cannot be converted to FlutterEngine
GeneratedPluginRegistrant.registerWith(this);
^
这是我的文件中显示的代码。GeneratedPluginRegistrant.java
package io.flutter.plugins;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.FlutterEngine;
@Keep
public final class GeneratedPluginRegistrant {
public static void registerWith(@NonNull FlutterEngine flutterEngine) {
flutterEngine.getPlugins().add(new com.example.trsurveys.TrsurveysPlugin());
}
}
在制作插件时,我尝试删除生成的PluginRegistrant.java文件,但每次使用相同的配置生成它时。