如何访问主屏幕的项目Widget
,以便对它们进行更改,例如设置Button
的文本?在我AppWidgetProvider
的onReceive()
方法中,我试图设置 a 的文本Button
,它位于我的主屏幕小部件中:
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(MainActivity.ACTION_CHANGE_BUTTONSTATE))
{
String state = intent.getExtras().getString("state");
Log.d("HomescreenWidget", "received broadcast for button state");
RemoteViews views = new RemoteViews(context.getPackageName(),
R.id.widgetButton);
views.setTextViewText(R.id.widgetButton, state);
ComponentName cn = new ComponentName(context,
HomescreenWidget.class);
AppWidgetManager.getInstance(context).updateAppWidget(cn, views);
}
super.onReceive(context, intent);
}
我的小部件layout.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/widget_margin" >
<Button
android:id="@+id/playButton_Widget"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:minWidth="80dp"
android:text="@string/playButton" />
</RelativeLayout>
我收到了Broadcast
,但随后我Widget
显示以下文本:“问题加载小部件”。
编辑 我在目录日志中发现了以下消息:
W/AppWidgetHostView(135): updateAppWidget couldn't find any view, using error view
W/AppWidgetHostView(135): android.widget.RemoteViews$ActionException: can't find view: 0x7f0a0004
0x7f0a0004
是我Widget
的Button
。可能是什么原因,找不到它?