java - Flutter java.io.File android.app.Activity.getExternalFilesDir(java.lang.String)' 在空对象引用上

标签 java android flutter background-service

在我的项目上安装 android_alarm_manager 后,我得到了这个错误..

I/AlarmService(24713): Starting AlarmService...
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713): Failed to handle method call
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713): java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.app.Activity.getExternalFilesDir(java.lang.String)' on a null object reference
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at io.flutter.plugins.imagepicker.ImagePickerPlugin.registerWith(ImagePickerPlugin.java:27)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:29)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at com.example.myapp.Application.registerWith(Application.java:18)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at io.flutter.plugins.androidalarmmanager.AlarmService.startAlarmService(AlarmService.java:65)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at io.flutter.plugins.androidalarmmanager.AndroidAlarmManagerPlugin.startService(AndroidAlarmManagerPlugin.java:75)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at io.flutter.plugins.androidalarmmanager.AndroidAlarmManagerPlugin.onMethodCall(AndroidAlarmManagerPlugin.java:52)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:191)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at io.flutter.view.FlutterNativeView.handlePlatformMessage(FlutterNativeView.java:163)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at android.os.MessageQueue.next(MessageQueue.java:143)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at android.os.Looper.loop(Looper.java:122)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at android.app.ActivityThread.main(ActivityThread.java:5253)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at java.lang.reflect.Method.invoke(Method.java:372)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
E/MethodChannel#plugins.flutter.io/android_alarm_manager(24713):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:708)                                      4.8s
🔥  To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on Mi 4i is available at: http://127.0.0.1:3860/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".
E/flutter (24713): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter (24713): PlatformException(error, Attempt to invoke virtual method 'java.io.File android.app.Activity.getExternalFilesDir(java.lang.String)' on a null object reference, null)
E/flutter (24713): #0      JSONMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:149:7)
E/flutter (24713): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:279:18)
E/flutter (24713): <asynchronous suspension>
E/flutter (24713): #2      AndroidAlarmManager.initialize (package:android_alarm_manager/android_alarm_manager.dart:70:10)
E/flutter (24713): <asynchronous suspension>

这是我的pubspec.yaml

dependencies:
  dio: "^1.0.1"
  flutter:
    sdk: flutter
  local_auth: "^0.2.0"
  shared_preferences: "^0.4.0"
  http: "^0.11.3+16"
  path_provider: ^0.4.1
  image_picker: "^0.4.10"
  multi_image_picker: "1.0.51"
  datetime_picker_formfield: ^0.1.3
  pull_to_refresh: ^1.1.5
  connectivity: ^0.3.1
  sqflite : any
  location: ^1.4.1
  android_alarm_manager: ^0.2.1
  geolocator: '^1.7.0'

我的main.dart

void main() async {
  final int msgId = 0;
  runApp(new LoginApp());
  await AndroidAlarmManager.initialize();
  await AndroidAlarmManager.periodic(
      const Duration(seconds: 5), msgId, autoResendReports);
}

void autoResendReports() {
  print("TEST");
  final DateTime now = new DateTime.now();
  final int isolateId = Isolate.current.hashCode;
  print(
      "[$now] Hello, world! isolate=$isolateId function='$autoResendReports'");
}

class LoginApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: '',
      theme: new ThemeData(
        primarySwatch: Colors.green,
      ),
      routes: routes,
    );
  }
}

我的 AndroidManifest 文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">

    <!-- The INTERNET permission is required for development. Specifically,
         flutter needs it to communicate with the running application
         to allow setting breakpoints, to provide hot reload, etc.
    -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.USE_FINGERPRINT"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name=".Application"
        android:label="MyApp"
        android:icon="@mipmap/ic_launcher2">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <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>
        <service
            android:name="io.flutter.plugins.androidalarmmanager.AlarmService"
            android:exported="false"/>
    </application>
</manifest>

我仍然无法让 flutter 工作的后台服务。请帮忙。

最佳答案

我有一段时间陷入了类似的问题,所以我尝试使用 java 文件解决问题。所以,基本上我所做的是在 android 端使用 java 创建了我想要的后台逻辑,这很容易做到。然后我使用 MethodChannel 与 flutter 的那一边交谈。我的应用程序只需要在应用程序计时器完成计数时发出警报,即使在后台也是如此。我在将变量传递给 android 端的服务时仍然存在一些问题,我认为这是次要的,但总的来说,后台的东西对我来说工作得很好。

关于java - Flutter java.io.File android.app.Activity.getExternalFilesDir(java.lang.String)' 在空对象引用上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52367347/

相关文章:

android - 底部导航 View 图标颜色显示不正确

dart - Flutter - 检测内容溢出并剪辑它

xcode - 互联网上没有适用于模块 'google_maps_flutter' 未找到的解决方案

android - Flutter:TextButton 中的 setState 不起作用

java - 如何使用 JSON.org 在 java 中反序列化 JSON

java - 谷歌搜索但找不到在 hibernate 中使用注释进行双向映射的解决方案

android - 选中的复选框不应取消选中

android - ExpandableListview组指标问题

java - 卡住最大堆空间选项

java - Android Java接口(interface)回调onPostExecute空指针异常