首页 > 解决方案 > 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文件中有什么东西导致了这个错误,但我不知道它是什么。请帮我。

标签: c#androidxamarinxamarin.androidwidget

解决方案


布局是一个问题,因为它没有定义布局参数,因此启动器不会正确膨胀。

试试这个有效的小部件布局作为开始:

<?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>

注意:您在原始布局中缺少srcfor your ...ImageView

在此处输入图像描述


推荐阅读