我正在尝试使用Java为Flutter创建一个插件。我试图在我的主要Java
类中传递Android activity
参数。
这是我的本地代码:
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());
}
}
在制作插件时,我尝试删除GeneratedPluginRegistrant.java文件,但每次它都会以相同的配置生成。