0

初めて単純なウィジェットを実行していますが、ウィジェットのポップアップリストから選択すると、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>
4

2 に答える 2

1

私の解決策:

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);
    }
}
于 2011-11-22T11:30:56.687 に答える
1

<LinearLayout>レイアウトファイルの開口部がありません。ここにあるものはコンパイルすらすべきではありません。

また、の実装全体が欠落していますAppWidgetProvideronUpdate()アプリウィジェットに表示する内容を指定するために実装する必要があります。

また、あなたupdatePeriodMillisは許可されているよりも短いです-この方法で10秒ごとにアプリウィジェットを更新することはできません。

また、レイアウトに名前が付けられていることを確認するか、レイアウトの適切な名前を反映するようにをmain.xml更新してください。android:initialLayout

于 2011-10-27T14:32:23.273 に答える