我试图在flutter应用程序中同时使用Speech_recognition和workmanager,但是由于某种原因,我无法使其正常工作。
如果我在没有workmanager的情况下使用Speech_recognition,只需将其添加到pubspec.yaml中,一切都可以正常工作
当我尝试使用workmanager时,它也可以工作。为此,除了pubspec.yaml依赖之外,还需要更多更改
在AndroidManifest.xml中,我进行了更改:
<application android:name="io.flutter.app.FlutterApplication" ... />
针对:
<application android:name=".Application" ... />
并创建Application.kt:
package com.company.app
import be.tramckrijte.workmanager.WorkmanagerPlugin
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.GeneratedPluginRegistrant
class Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
WorkmanagerPlugin.setPluginRegistrantCallback(this)
}
override fun registerWith(reg: PluginRegistry?) {
GeneratedPluginRegistrant.registerWith(reg)
}
}
扑打医生:
[✓] Flutter (Channel stable, v1.9.1+hotfix.6, on Linux, locale es_ES.UTF-8)
• Flutter version 1.9.1+hotfix.6 at /home/fsalguero/software/flutter
• Framework revision 68587a0916 (hace 3 meses), 2019-09-13 19:46:58 -0700
• Engine revision b863200c37
• Dart version 2.5.0
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at /home/fsalguero/Android/Sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 29.0.2
• Java binary at: /home/fsalguero/software/android-studio/jre/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
• All Android licenses accepted.
[✓] Android Studio (version 3.5)
• Android Studio at /home/fsalguero/software/android-studio
• Flutter plugin version 41.1.2
• Dart plugin version 191.8593
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
[✓] VS Code (version 1.40.2)
• VS Code at /usr/share/code
• Flutter extension version 3.7.1
[✓] Connected device (1 available)
• SM A505FN • R58M76BADQK • android-arm64 • Android 9 (API 28)
• No issues found!
来自pubspec.yaml的版本:
speech_recognition: ^0.3.0+1
workmanager: ^0.1.5
执行时出现此错误:
D/AndroidRuntime( 3241): Shutting down VM
E/AndroidRuntime( 3241): FATAL EXCEPTION: main
E/AndroidRuntime( 3241): Process: com.zink.trackely, PID: 3241
E/AndroidRuntime( 3241): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Activity.getApplicationContext()' on a null object reference
E/AndroidRuntime( 3241): at bz.rxla.flutter.speechrecognition.SpeechRecognitionPlugin.<init>(SpeechRecognitionPlugin.java:46)
E/AndroidRuntime( 3241): at bz.rxla.flutter.speechrecognition.SpeechRecognitionPlugin.registerWith(SpeechRecognitionPlugin.java:38)
E/AndroidRuntime( 3241): at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:40)
E/AndroidRuntime( 3241): at com.zink.trackely.Application.registerWith(Application.kt:15)
E/AndroidRuntime( 3241): at be.tramckrijte.workmanager.BackgroundWorker$doWork$1.run(BackgroundWorker.kt:75)
E/AndroidRuntime( 3241): at android.os.Handler.handleCallback(Handler.java:873)
E/AndroidRuntime( 3241): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3241): at android.os.Looper.loop(Looper.java:214)
E/AndroidRuntime( 3241): at android.app.ActivityThread.main(ActivityThread.java:7076)
E/AndroidRuntime( 3241): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 3241): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/AndroidRuntime( 3241): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
I/Process ( 3241): Sending signal. PID: 3241 SIG: 9
Lost connection to device.