c# - Android - 小部件未加载
问题描述
我正在尝试在我的C#
Android 应用程序中实现一个简单的小部件。但是运行我的代码后的小部件正在显示此消息:
加载小部件时出现问题。
appwidgetprovider.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="288dip"
android:minHeight="72dip"
android:resizeMode="horizontal"
android:minResizeWidth="144dip"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/Widget"
android:previewImage="@drawable/Icon" />
应用小部件.cs
[BroadcastReceiver(Label = "My Widget")]
[IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_UPDATE" })]
[MetaData("android.appwidget.provider", Resource = "@xml/appwidgetprovider")]
public class AppWidget : AppWidgetProvider
{
public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
var me = new ComponentName(context, Java.Lang.Class.FromType(typeof(AppWidget)).Name);
appWidgetManager.UpdateAppWidget(me, BuildRemoteViews(context, appWidgetIds));
}
private RemoteViews BuildRemoteViews(Context context, int[] appWidgetIds)
{
var widgetView = new RemoteViews(context.PackageName, Resource.Layout.Widget);
SetTextViewText(widgetView);
RegisterClicks(context, appWidgetIds, widgetView);
return widgetView;
}
private void SetTextViewText(RemoteViews widgetView)
{
widgetView.SetTextViewText(Resource.Id.widgetMedium, "HelloAppWidget");
widgetView.SetTextViewText(Resource.Id.widgetSmall,
string.Format("Last update: {0:H:mm:ss}", DateTime.Now));
}
private static string AnnouncementClick = "AnnouncementClickTag";
private void RegisterClicks(Context context, int[] appWidgetIds, RemoteViews widgetView)
{
var intent = new Intent(context, typeof(AppWidget));
intent.SetAction(AppWidgetManager.ActionAppwidgetUpdate);
intent.PutExtra(AppWidgetManager.ExtraAppwidgetIds, appWidgetIds);
// Register click event for the Background
var piBackground = PendingIntent.GetBroadcast(context, 0, intent, PendingIntentFlags.UpdateCurrent);
widgetView.SetOnClickPendingIntent(Resource.Id.widgetAnnouncementIcon, GetPendingSelfIntent(context, AnnouncementClick));
}
private PendingIntent GetPendingSelfIntent(Context context, string action)
{
var intent = new Intent(context, typeof(AppWidget));
intent.SetAction(action);
return PendingIntent.GetBroadcast(context, 0, intent, 0);
}
public override void OnReceive(Context context, Intent intent)
{
base.OnReceive(context, intent);
// Check if the click is from the "Announcement" button
if (AnnouncementClick.Equals(intent.Action))
{
// Open another app
}
}
小部件.axml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widgetBackground">
<RelativeLayout android:layout_width="72dip">
<ImageView
android:id="@+id/widgetAnnouncementIcon" />
</RelativeLayout>
<LinearLayout>
<TextView
android:id="@+id/widgetMedium"
android:text="HelloAppWidget" />
<TextView
android:id="@+id/widgetSmall"
android:text="Last refresh: 00:00" />
</LinearLayout>
</LinearLayout>
我已经在网上搜索了解决方案并尝试了它们,但这个错误仍然存在。我怀疑我的layout
文件中有什么东西导致了这个错误,但我不知道它是什么。请帮我。
解决方案
布局是一个问题,因为它没有定义布局参数,因此启动器不会正确膨胀。
试试这个有效的小部件布局作为开始:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widgetBackground"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ImageView
android:layout_width="72dp"
android:layout_height="72dp"
android:id="@+id/widgetAnnouncementIcon"
android:src="@drawable/oj" />
<LinearLayout
android:layout_toRightOf="@id/widgetAnnouncementIcon"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/widgetMedium"
android:text="HelloAppWidget" />
<TextView
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/widgetSmall"
android:text="Last refresh: 00:00" />
</LinearLayout>
</RelativeLayout>
注意:您在原始布局中缺少src
for your ...ImageView
推荐阅读
- mysql - 使用 MySQL 分块删除
- 2sxc - 缺少可视查询选项卡
- node.js - 如何避免总是必须在 Typescript 中导入自己的代码?
- laravel - Laravel Auth role_id 会话仅覆盖此会话
- javascript - 试图访问字符串数组,说它是空的,即使它已满
- c# - 如何从 .cs 文件加载类并在代码中使用它
- firebase - Angular 5 + Firebase:命名空间“firebase.auth”没有导出成员“IdTokenResult”
- sql - 如果查询结果在 BigQuery 中没有记录,则显示默认值
- templates - Helm 图表嵌套循环
- javascript - Rails 5 / JavaScript:仅动态更新页面上已更新的记录属性