我正在尝试在我的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" />
Appwidget.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
}
}
Widget.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
文件中有什么内容会导致此错误,但我不知道它是什么。请帮我。
答案 0 :(得分:0)
布局是一个问题,因为它没有定义布局参数,因此不会被启动器正确充气。
尝试将此有效窗口小部件布局作为启动器:
<?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
的{{1}} ...