相反,小部件区域显示一条错误消息,提示“加载小部件时出现问题”
在日志中,添加应用小部件时看到这些错误:
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)
该小部件在Android 8之前的版本中显示正常
编辑:这是我的小部件布局文件:
<?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" />
答案 0 :(得分:0)
“加载小部件时出现问题” +您有限的堆栈跟踪指示布局问题。如果您显示布局文件,这将有所帮助。返回并检查所有属性,图像和视图。您的版式中可能有一个元素不允许应用程序使用,因为它们限制了可以使用哪些视图。还要检查您是否在元数据xml文件中正确设置了窗口小部件的大小,并检查填充是否遵循准则。 android准则描述了如何调整其大小并使用填充:https://developer.android.com/guide/practices/ui_guidelines/widget_design