So here is my 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;
}
}
And here is the widget layout.
<?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>
The Problem:
The widget works just fine when the allMessages
array has a length of zero: I set the text of the TextView to "There are no messages to show" and everyone is happy; when I have at least one message in the array, the code runs just fine, no exceptions, no nothing (I debugged it and everything) but when onUpdate
returns, the emulator shows "Problem loading widget" on the home screen where the widget should be. It is also important to notice that the same exact code works just fine in Froyo and Gingerbread. Why am I not getting any exceptions if something is going wrong? The debugger runs the entire onUpdate
method and returns just fine, but the widget wont load. Any ideas?