【发布时间】:2018-05-10 09:47:49
【问题描述】:
我正在制作一个非常简单的小部件,它使用 ImageView 显示图像。
我的布局看起来像这样
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dip">
<!-- ImageView logo -->
<ImageView
android:id="@+id/logo"
android:src="@drawable/giants"
android:contentDescription="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitXY" />
</RelativeLayout>
我的 Widget Provider 类如下所示
public class LogoWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds) {
// What Happens While App Goes Through Each Widget Id//
for (int currentWidgetId: appWidgetIds) {
// Define And Instantiate Variable RemoteViews widgetLogo//
RemoteViews widgetLogo = new RemoteViews(context.getPackageName(), R.layout.widget_logo);
// Update Widget//
appWidgetManager.updateAppWidget(currentWidgetId, widgetLogo);
}
}
}
清单中的接收器如下所示
<!-- Widget Provider Logo -->
<receiver android:name=".LogoWidgetProvider">
<!-- Tell The App That There Is A Widget To Display -->
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<!-- Tell The App Where To Find Widget Data -->
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info_logo">
</meta-data>
</receiver>
最后,小部件提供程序 XML 如下所示
<!-- Basic Information For Widget Logo -->
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_logo"
android:minHeight="110dip"
android:minWidth="110dip"
android:minResizeWidth="40dip"
android:resizeMode="vertical|horizontal"
android:updatePeriodMillis="3600000"
android:widgetCategory="home_screen">
</appwidget-provider>
由于某种原因,小部件每次都会崩溃。如果我要将 ImageView 切换到 Button 它工作正常。让小部件显示图像我缺少什么?
谢谢
【问题讨论】:
-
你也可以发布堆栈跟踪吗?
-
@MykhailoYuzheka 遗憾的是,堆栈跟踪中没有任何内容可显示。当我将其放在主屏幕上时,应用程序本身不会仅崩溃小部件
-
你应该用你的问题标题来描述问题。这样,更有经验的开发人员更容易发现他们专业领域的问题。此外,您应该尝试提出较短的问题,因为他们会更快地得到答案,并且答案的质量会更好
标签: java android imageview widget