gpt4 book ai didi

java - Flutter:MainActivity 在 Java Native 代码中无法转换为 FlutterEngine

转载 作者:行者123 更新时间:2023-12-03 13:26:11 25 4
gpt4 key购买 nike

我正在尝试用 Java 为 Flutter 创建一个插件。我正在尝试通过 Android activity我的主要参数 Java类(class)。

这是我的 native 代码:

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 文件,但每次生成时都使用相同的配置。

最佳答案

打开您的项目 list 文件并删除此行

meta-data
android:name=”flutterEmbedding”
android:value=”2″

然后再次构建并运行它。

关于java - Flutter:MainActivity 在 Java Native 代码中无法转换为 FlutterEngine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61853913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com