【问题标题】:Updating appWidget gives Problem loading gadget更新 appWidget 会导致加载小工具出现问题
【发布时间】:2011-02-24 07:58:48
【问题描述】:

我正在尝试向我的主 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>

【问题讨论】:

  • 错误是什么?你能把堆栈跟踪吗?
  • 小部件不显示,而是在小部件应该在的位置显示加载小工具时出现问题。 Log.e 上没有显示(我没有用自己的 Log 测试,因为我不知道要测试什么)

标签: android android-widget android-appwidget


【解决方案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的人来说只是一个指针。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        相关资源
        最近更新 更多