Ошибка надувания AppWidget - PullRequest
       24

Ошибка надувания AppWidget

0 голосов
/ 27 октября 2011

Я делаю простой виджет впервые, и мне кажется, что в учебнике, который я использую, чего-то не хватает, потому что мой LogCat заявляет «Ошибка надувания AppWidget» при выборе его из всплывающего списка виджетов.

В соответствии с руководством я сделал это.

Макет:

<TextView android:id="@+id/widget_textview" android:text="@string/widget_text"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:layout_marginTop="5dip" android:padding="10dip"
    android:textColor="@android:color/black" />
</LinearLayout>

Класс:

package hello.widget;

import android.appwidget.AppWidgetProvider;

public class HelloWidget extends AppWidgetProvider {
}

Строки:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Hello Widget</string>
    <string name="widget_text">Hello Widget!</string>
</resources>

Поставщик виджетов:

<?xml version="1.0" encoding="utf-8"?>
appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android">
    android:minWidth="146dip"
    android:minHeight="72dip"
    android:updatePeriodMillis="10000"
    android:initialLayout="@layout/main"
</appwidget-provider>

Манифест:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hello.widget" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">

    <!-- Broadcast Receiver that will process AppWidget updates -->
    <receiver android:name="hello.widget.HelloWidget" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/hello_widget_provider" />
    </receiver>

    </application>
</manifest>

Ответы [ 2 ]

1 голос
/ 22 ноября 2011

Мое решение:

public void onReceive(Context context, Intent intent)
{
    String action = intent.getAction();

    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
    {   
        long days = (((calendar.getTimeInMillis()- date1.getTime())/1000))/86400;
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

        //Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.android.alarmclock", "com.android.alarmclock.AlarmClock"));
        // PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0);
        Intent Date_Change= new Intent(Intent.ACTION_DATE_CHANGED);
        PendingIntent pendingIntent2=PendingIntent.getActivity(context,0, Date_Change, 0);

        views.setOnClickPendingIntent(R.id.textView1, pendingIntent2);
        views.setTextViewText(R.id.textView1,""+days);

        //views.setOnClickPendingIntent(R.id.AnalogClock, pendingIntent);

        //AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        ComponentName thisWidget = new ComponentName(context, Widget.class);
        appWidgetManager.updateAppWidget(thisWidget, views);
    }
}
1 голос
/ 27 октября 2011

Вам не хватает открытия <LinearLayout> в файле макета. То, что у вас здесь, не должно даже компилироваться.

Вам также не хватает всей реализации вашего AppWidgetProvider. Вам нужно реализовать onUpdate(), чтобы указать, какой виджет приложения должен отображаться.

Кроме того, ваш updatePeriodMillis короче допустимого - таким образом вы не можете обновлять виджет приложения каждые 10 секунд.

Кроме того, убедитесь, что ваш макет имеет имя main.xml, или обновите свой android:initialLayout, чтобы отразить правильное имя макета.

...