所以这是我的WidgetProvider
public class MessagesWidgetProvider extends AppWidgetProvider {
public static final String EXTRA_WIDGET_MESSAGE = "widget_message";
public static final String EXTRA_WIDGET_ID = "widget_id";
@Override
public void onUpdate(Context context,
AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
updateWidget(context, appWidgetIds, appWidgetManager);
}
private static void updateWidget(Context context, int[] appWidgetIds,
AppWidgetManager appWidgetManager) {
ForwardingMessage[] allMessages = ForwardingMessage.getAllMessages();
RemoteViews views = null;
if (allMessages.length == 0) {
views = new RemoteViews(context.getPackageName(),
R.layout.messages_widget_layout);
views.setTextViewText(R.id.profileNameTextView,
context.getString(R.string.no_messages_to_show));
views.setTextViewText(R.id.messageContentTextView, "");
appWidgetManager.updateAppWidget(appWidgetIds, views);
} else {
ForwardingMessage latestMessage = allMessages[allMessages.length - 1];
for (int id : appWidgetIds) {
views = generateViewsFromMessage(latestMessage, context, id);
appWidgetManager.updateAppWidget(id, views);
}
}
}
private static RemoteViews generateViewsFromMessage(
ForwardingMessage message, Context context, int widgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.messages_widget_layout);
ForwardingMessage previous = message.getPreviousMessage();
ForwardingMessage next = message.getNextMessage();
views.setBoolean(R.id.previousButton, "setEnabled", previous != null);
views.setBoolean(R.id.nextButton, "setEnabled", next != null);
views.setTextViewText(R.id.profileNameTextView, message
.getForwardingProfile().getName());
views.setTextViewText(R.id.messageContentTextView, message.getText());
// Let's start adding the listeners to the buttons
if (previous != null) {
Intent previousIntent = new Intent(ACTION_SHOW_WIDGET_MESSAGE);
previousIntent.putExtra(EXTRA_WIDGET_MESSAGE, previous);
previousIntent.putExtra(EXTRA_WIDGET_ID, widgetId);
PendingIntent previousPendingIntent = PendingIntent.getBroadcast(
context, 0, previousIntent, PendingIntent.FLAG_ONE_SHOT
| PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.previousButton,
previousPendingIntent);
}
// Now the next message
if (next != null) {
Intent nextIntent = new Intent(ACTION_SHOW_WIDGET_MESSAGE);
nextIntent.putExtra(EXTRA_WIDGET_MESSAGE, next);
nextIntent.putExtra(EXTRA_WIDGET_ID, widgetId);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(
context, 1, nextIntent, PendingIntent.FLAG_ONE_SHOT
| PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.nextButton, nextPendingIntent);
}
return views;
}
}
这是小部件布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AA000000" android:layout_margin="10dp">
<TextView
android:id="@+id/profileNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/messageContentTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/profileNameTextView"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#FFFFFF" android:layout_margin="5dp" android:scrollbars="vertical"/>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<Button
android:id="@+id/previousButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/previous" />
<Button
android:id="@+id/nextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/next" />
</LinearLayout>
</RelativeLayout>
问题:
当allMessages
数组的长度为零时,窗口小部件工作得很好:我将TextView的文本设置为“没有要显示的消息”,每个人都很开心;当我在数组中至少有一条消息时,代码运行得很好,没有异常,没有任何东西(我调试了它和所有东西)但是当onUpdate
返回时,模拟器在主屏幕上显示“Problem loading widget”小部件应该在哪里。同样重要的是要注意相同的代码在Froyo和Gingerbread中工作得很好。如果出现问题,为什么我没有得到任何例外?调试器运行整个onUpdate
方法并返回正常,但小部件不会加载。有什么想法吗?