0

我正在尝试向我的主 RemoteViews 添加一个视图,但我不断收到加载小工具错误的问题。这是xmls和代码,将不胜感激。

public class WidgetProvider extends AppWidgetProvider {


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);
    RemoteViews bookmark = new RemoteViews(context.getPackageName(), R.layout.widget_items);
    updateViews.addView(R.id.view_container,  bookmark);

    appWidgetManager.updateAppWidget(thisWidget, updateViews);

}

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout_widget"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/widget_4x4_portrait"
>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/view_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <!-- New views will be added here at runtime -->
    </LinearLayout>
</LinearLayout>
4

3 回答 3

1

另请注意,2.1 有一个错误,其中

appWidgetManager.updateAppWidget(int ID, RemoteViews rv)

不起作用,您必须使用

appWidgetManager.updateAppWidget(int [] IDs, RemoteView rv).

我只是简单地制作了一个单位长度数组,其中我的特定widget ID元素是唯一的元素。

奇怪的是,这两种方法在1.6, 2.0, 2.2 and 2.3.

只是对其他坚持不更新的人的指针appWidgets

于 2011-08-03T05:57:10.767 回答
0

我想你可以试试appWidgetManager.getInstance(context).updateAppWidget(thisWidget, updateViews);of appWidgetManager.updateAppWidget(thisWidget, updateViews);

于 2011-02-24T08:26:38.400 回答
0

我发现问题出在 addView 调用中,我将新视图添加到自身而不是小部件上的主布局。

于 2011-02-24T08:31:59.213 回答