TextView小部件错误加载小部件

时间:2013-02-21 19:44:44

标签: android android-widget textview timertask

我的小部件在尝试使用它时给了我“问题加载小部件”,我不知道为什么。

这是我的代码:

    public class DigitalClock extends AppWidgetProvider {

public void onUpdate(Context context,
        final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    final RemoteViews views = new RemoteViews(context.getPackageName(),
            R.layout.digitalclock);

    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];

        Intent clockIntent = new Intent(context, DeskClock.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                clockIntent, 0);

        views.setOnClickPendingIntent(R.id.gl, pendingIntent);

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new UpdateTimeTask(context,
                appWidgetManager), 1, 1000);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

private class UpdateTimeTask extends TimerTask {
    RemoteViews views;
    AppWidgetManager appWidgetManager;
    ComponentName thisWidget;

    public UpdateTimeTask(Context context, AppWidgetManager appWidgetManager) {
        this.appWidgetManager = appWidgetManager;
        views = new RemoteViews(context.getPackageName(), R.id.gl);
        thisWidget = new ComponentName(context, DigitalClock.class);
    }

    @Override
    public void run() {
        java.util.Date noteTS = Calendar.getInstance().getTime();
        String time = "kk:mm";
        String date = "dd MMMMM yyyy";

        views.setTextViewText(R.id.tvTime, DateFormat.format(time, noteTS));

        views.setTextViewText(R.id.tvDate, DateFormat.format(date, noteTS));

        appWidgetManager.updateAppWidget(thisWidget, views);
    }

}

    }

XML布局

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:id="@+id/gl"
    tools:ignore="UselessParent" >

    <TextView
        android:id="@+id/tvTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/time"
        android:textColor="#cccccc"
        android:textSize="80sp" />

    <TextView
        android:id="@+id/tvDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:text="@string/date"
        android:textColor="#cccccc"
        android:textSize="20sp" />
</GridLayout>

</GridLayout>

清单

        <receiver
        android:name="com.timmo.clock.DigitalClock"
        android:label="@string/app_name_dc" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="com.timmo.clock.ACTION_WIDGET_RECEIVER" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/digitalclocksettings" />
    </receiver>

我在某处出错了吗?或者计时器方法是更新我的TextView的错误方法吗?

1 个答案:

答案 0 :(得分:0)

一个应用程序有多个小部件。你的“thisWidget”是错误的。一个小部件由小部件ID定义,它由Android生成,您可以通过以下方式获取:

  mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

在配置小部件活动或

ComponentName cn = new ComponentName(context,
                MoneyWidgetAccountDark.class);
        int[] appWidgets = appWidgetManager.getAppWidgetIds(cn);

        for (int appWidget : appWidgets) {

在此处插入用于更新textview的代码!