更新appWidget会出现问题加载小工具

时间:2011-02-24 07:58:48

标签: android android-widget android-appwidget

我正在尝试将视图添加到我的主要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>

3 个答案:

答案 0 :(得分: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的人。

答案 1 :(得分:0)

我认为您可以尝试:appWidgetManager.getInstance(context).updateAppWidget(thisWidget, updateViews);而不是of appWidgetManager.updateAppWidget(thisWidget, updateViews);

答案 2 :(得分:0)

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