【发布时间】:2021-10-28 04:44:06
【问题描述】:
我正在开发一个必须调用 java 函数的颤振项目。所以,我这样定义了我的 MainActivity.java:
package com.manager.nlp;
import android.os.Bundle;
import android.content.Intent;
import android.os.PowerManager;
import android.os.Build.VERSION;
import androidx.annotation.NonNull;
import android.content.IntentFilter;
import android.content.ContextWrapper;
import android.os.Build.VERSION_CODES;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.embedding.android.FlutterActivity;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "analisisSintactico";
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler((call, result) -> {
if (call.method.equals("method")) {
result.success("value1");
} else {
result.success("value2");
}
});
}
}
我称它为从 dart 的主文件中执行此操作:
(我会跳过不重要的,比如其他小部件和那些东西)
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:flutter/services.dart';
class PaginaHome extends StatefulWidget {
const PaginaHome({Key key}) : super(key: key);
@override
_PaginaHomeState createState() => _PaginaHomeState();
}
class _PaginaHomeState extends State<PaginaHome> {
static const cajaNegra = const MethodChannel('analisisSintactico');
@override
Widget build(BuildContext context) {
return Scaffold(
body: new Column(children: <Widget>[
IconButton(
onPressed: () {
invocarAJava();
}
...
)
...
)
}
Future<void> invocarAJava() async {
String valor = await cajaNegra.invokeMethod('method');
print(valor);
}
}
当点击那个图标按钮时,我得到这个错误:
E/flutter (9060): [错误:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:MissingPluginException(未找到实现 对于通道 analisisSintactico 上的方法方法)E/flutter(9060):#0 MethodChannel._invokeMethod (包:flutter/src/services/platform_channel.dart:156:7) E/flutter ( 9060):E/颤振(9060):#1
_PaginaHomeState.invocarAJava (package:holamundo/main.dart:146:20) E/flutter (9060):
如果我运行“flutter doctor -v”,它说一切都是正确的
我还尝试执行flutter clean,然后flutter run,从调试android模拟器中删除了de apk。它仍然显示相同的错误。
我知道文件正在被评估,因为如果我写了一些愚蠢的东西,它会检测到错误。但是,不知何故,它无法识别它正在调用的方法。还尝试了其他名称的通道和方法。
最后,这是我的 build.gradle:
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 30
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
main.java.srcDirs += 'src/main/java'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.holamundo"
minSdkVersion 21
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
minifyEnabled true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
如果有人知道是什么问题导致我无法调用我的 java 代码,我很乐意接受任何建议。谢谢。
【问题讨论】:
标签: java android flutter dart channels