Flutter 项目中自定义 Android widget 的类未找到错误

Class not found error for a custom Android widget in a Flutter project

提问人:Marco Lazzaroni 提问时间:11/9/2023 最后编辑:Marco Lazzaroni 更新时间:11/9/2023 访问量:62

问:

我是 Flutter 的初学者,我正在尝试在 Flutter 项目中创建一个自定义的 Android 小部件。我遇到错误“java.lang.RuntimeException:无法实例化接收器com.example.flutter_ure。ClockWidget“,当我尝试选择我的小部件时。这是我已经检查并完成的内容:

  1. 我创建了一个扩展 AppWidgetProvider 的类 ClockWidget。该类位于 android/app/src/main/java/com/example/flutter_ure/ClockWidget.java
  2. 我在我的AndroidManifest.xml文件中定义了接收器,如下所示:
<receiver
    android:name="com.example.flutter_ure.ClockWidget"
    android:label="Clock"
    android:exported="true">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/widget_clock" />
</receiver>

  1. 我已确保我的 AndroidManifest.xml 中指定的包与我的 Flutter 项目 (com.example.flutter_ure) 中的包匹配。
  2. ClockWidget.java 文件包括 R 类的正确导入:
import com.example.flutter_ure.R;

但是,错误仍然存在,我无法使我的自定义小部件正常工作。我做错了什么?如何解决此问题?

我将不胜感激任何帮助或建议来解决此错误。先谢谢你。

--编辑: 为任何想使用它的人添加了一个 GitHub 存储库:https://github.com/motumboe/flutter_ure

Android Flutter 小部件 接收器

评论

1赞 tomerpacific 11/9/2023
这回答了你的问题吗?是否可以在 flutter 中构建 android 小部件?
0赞 Marco Lazzaroni 11/9/2023
提供的链接是六年前的,也许从那时起情况发生了变化。我设法在智能手机的主屏幕上添加了一个小部件(就像widget_clock.xml一样,我得到了一个黄色的“Hello World”文本)。但是,当我尝试更新它时,我遇到了提到的错误。但是,小部件仍然存在,显示原始的“Hello World”文本。
0赞 tomerpacific 11/10/2023
等等,如果你设法在 Flutter 中制作了一个可以工作的自定义 android 小部件,那么你做了什么更改,以至于它现在被破坏了?
0赞 Marco Lazzaroni 11/10/2023
这不是一个两步操作:参考这个项目,github.com/motumboe/flutter_ure,我能够在主屏幕上放置一个小部件(默认文本如widget_clock.xml“Hello World :-)”中定义),但我无法每秒更新它(我得到上述错误)。我什至无法删除更新调用以避免错误

答:

0赞 tomerpacific 11/9/2023 #1

根据文档,接收器的名称应该是小部件类的名称,而不是类的包

例如

<receiver android:name="ExampleAppWidgetProvider"
                 android:exported="false">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/example_appwidget_info" />
</receiver>

因此,在您的案例中,它看起来像这样:

<receiver
    android:name="ClockWidget"
    android:label="Clock"
    android:exported="true">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/widget_clock" />
</receiver>

评论

0赞 Marco Lazzaroni 11/9/2023
嗨,Tomer,非常感谢您的回复!我已经按照建议编辑了android\app\src\main\AndroidManifest.xml,但我仍然感到抱歉,但我不知道该去哪里看E/AndroidRuntime(32248): java.lang.RuntimeException: Unable to instantiate receiver com.example.flutter_ure.ClockWidget: java.lang.ClassNotFoundException: Didn't find class "com.example.flutter_ure.ClockWidget" on path (...)
0赞 tomerpacific 11/9/2023
@MarcoLazzaroni - 您能否使用更新的清单文件更新您的问题?另外,您能分享一下课程本身吗?
0赞 Marco Lazzaroni 11/9/2023
为任何想使用它的人添加了一个 GitHub 存储库:github.com/motumboe/flutter_ure