Flutter:MainActivity 不能在 Java Native 代码中转换为 FlutterEngine

我正在尝试在Java中为Flutter创建一个插件。我正在尝试在我的主类中传递参数。Android activityJava

这是我的原生代码:

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


答案 1

当我将存储库从较旧的flutter版本升级到较新的版本时,我遇到了这种情况。我按照官方的飘飘链接进行迁移。

我对Android清单文件进行了适当的更改。即:

  1. 从标记中删除属性android:name<application>
  2. 将单曲更改为android:name<activity>android:name="io.flutter.embedding.android.FlutterActivity"
  3. 添加为标记的子级<meta-data android:name="flutterEmbedding" android:value="2"/>

但是,我没有更改导致错误的任何代码。使用的是较旧的进口产品,这与上述三项更改不兼容。MainActivityMainActivity

最终清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.appbrewery.magic8ball">
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:label="magic_8_ball"
    android:icon="@mipmap/ic_launcher">
    <activity
        android:name="io.flutter.embedding.android.FlutterActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <meta-data android:name="flutterEmbedding" android:value="2"/>
        <meta-data
            android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
            android:value="true" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

删除 为我解决了问题。MainActivity


答案 2

打开项目清单文件并删除此行

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

然后再次生成并运行它。


推荐