2014-11-23 59 views
0

當我啓動我的小部件,意圖將一個意圖附加到按鈕。意圖啓動一個服務,目前只是做一個敬酒。我不認爲這是我的錯誤,因爲我檢查了很多次,而我的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提供者:

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) 

回答

0

替換:

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

有:

for (int appWidgetId : appWidgetIds) { 

如果您使用增強for語法遍歷集合,你迭代的集合,而不是數組的索引值。

+0

好點但是這並不能解決錯誤 – cheese12345 2014-11-23 18:58:53

+0

@ cheese12345:它消除了你的一次性數組訪問,並且你不能在不訪問數組的情況下產生'ArrayIndexOutOfBoundsException'。您可能有* *不同*錯誤,但沒有人可以幫助您,因爲我們不知道您當前的代碼是什麼樣的,我們不知道您當前的異常是什麼。 – CommonsWare 2014-11-23 19:04:49