【问题标题】:App widget not displaying on Oreo应用小部件未在奥利奥上显示
【发布时间】:2018-07-25 22:33:12
【问题描述】:

相反,小部件区域会显示一条错误消息“加载小部件时出现问题”

在日志中,我在添加应用小部件时看到了这些错误:

07-24 15:57:24.651 2057-5433/? E/RemoteViews: Package name com.theapp not found

07-24 15:57:24.651 2057-2057/? W/AppWidgetHostView: updateAppWidget couldn't find any view, using error view
    android.content.res.Resources$NotFoundException: Resource ID #0x7f0b0034 type #0x5 is not valid
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2139)
        at android.content.res.Resources.getLayout(Resources.java:1143)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
        at android.widget.RemoteViews.inflateView(RemoteViews.java:3498)
        at android.widget.RemoteViews.-wrap1(Unknown Source:0)
        at android.widget.RemoteViews$AsyncApplyTask.doInBackground(RemoteViews.java:3600)
        at android.widget.RemoteViews$AsyncApplyTask.doInBackground(RemoteViews.java:3596)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

小部件在 8 之前的 Android 版本上显示良好

编辑:这是我的小部件布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/appwidget_background"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/button_settings"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/app_name" />

        <TextView
            android:id="@+id/button_on"
            android:layout_width="56dp"
            android:layout_height="match_parent"
            android:text="@string/on" />

        <TextView
            android:id="@+id/button_off"
            android:layout_width="56dp"
            android:layout_height="match_parent"
            android:text="@string/off" />
    </LinearLayout>
</FrameLayout>

和元数据文件:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="40dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="0"
    android:previewImage="@drawable/widget_preview"
    android:configure="com.theapp.appwidget.AppWidgetActivity"
    android:initialLayout="@layout/appwidget"
    android:resizeMode="horizontal|vertical" />

【问题讨论】:

  • 嗯,堆栈跟踪显示它正在尝试将R.dimen 加载为R.layout。如果它在以前的版本上运行,我真的无法想象这是怎么发生的。
  • 你怎么知道它正在尝试加载 R.dimen ? #0x5 类型是什么?
  • 一个R.dimendeveloper.android.com/reference/android/util/…0x7f0b0034 在您的R.java 中分配给什么?如果你在上面的堆栈跟踪之后重建,那可能已经改变了,所以在你去挖掘之前检查一下重新运行。
  • 是的,我发现另一个问题与完全相同的问题,但它已被删除,所以你看不到它。 (在此处留下一个链接以供我将来参考:stackoverflow.com/q/51272322)他们说它在带有 Android 8.1 的 Pixel 2 上失败了,但在 9.0 上它工作得很好。如果您正在运行 8.1 并且可以升级,我不确定您可以做些什么,除了升级之外。
  • 当然,我想这只会对您的个人设备有帮助。不会帮助您的用户解决问题,除非他们可以升级并愿意升级。

标签: android android-widget


【解决方案1】:

“加载小部件时出现问题”+您有限的堆栈跟踪表明存在布局问题。如果您显示您的布局文件会有所帮助。返回并检查您的所有属性、图像和视图。您的布局中可能有一个元素不允许用于应用程序小部件,因为它们在可以使用的视图中受到限制。还要检查您在元数据 xml 文件中是否正确调整了小部件的大小,并检查您的填充是否遵循指南。 android 指南描述了你应该如何调整它的大小和使用填充:https://developer.android.com/guide/practices/ui_guidelines/widget_design

【讨论】:

  • 我已经更新了布局文件和元数据 xml 文件,因此它们遵循尺寸限制,但我仍然在小部件中看到错误消息
  • 我对您的布局进行了测试(没有自定义可绘制对象,因为我没有),它对我有用。您的自定义背景可绘制是什么?如果它是矢量可绘制对象,那是您的问题,appwidgets 将不支持这些。可绘制的形状应该没问题。顺便说一句,测试的设备是 v8 Oreo
  • 我还注意到您没有为边距设置动态填充。根据指南,您应该创建两个 dimen.xml 文件,一个用于您的 values 文件夹,另一个用于 values-v14,其中 v14 尺寸设置为 0dp,另一个设置为 8dp。有关更多详细信息,请参阅此页面:developer.android.com/guide/topics/appwidgets/#CreatingLayout
  • 感谢@RebeccaMcBath,我检查了背景drawable并添加了dimens.xml文件,但同样的问题仍然存在......
猜你喜欢
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多