应用程序小部件提供程序崩溃

时间:2014-11-23 18:46:53

标签: java android android-widget

当我启动我的小部件时,意图将Intent附加到按钮上。意图启动了一项服务,目前只是举杯祝酒。我不认为我的清单是错误的,因为我多次检查它并且我的IDE没有显示任何错误。

清单:

    <service android:name=".Killer"></service>

    <receiver android:name=".Provider">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/app_widget_info"/>
    </receiver>  

AppWidget Provider:

public class Provider extends AppWidgetProvider{

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i : appWidgetIds) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch Killer
            Intent intent = new Intent(context, Killer.class);

            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            views.setRemoteAdapter(R.id.kill, intent);

            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

服务:

public class Killer extends Service{

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

LogCat错误:

java.lang.RuntimeException: Unable to start receiver uk.co.clickcomputing.simpleappkillerwidget.Provider: java.lang.ArrayIndexOutOfBoundsException: length=1; index=111
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:2674)
            at android.app.ActivityThread.access$1800(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1383)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=111
            at uk.co.clickcomputing.simpleappkillerwidget.Provider.onUpdate(Provider.java:16)
            at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:2659)
            at android.app.ActivityThread.access$1800(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1383)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

替换:

for (int i : appWidgetIds) {
        int appWidgetId = appWidgetIds[i];

使用:

for (int appWidgetId : appWidgetIds) {

如果使用增强的for语法迭代集合,则迭代集合的值,而不是数组索引。