2013-03-27 80 views
3

請幫忙。

Android: Unable to instantiate service...can't instantiate class com.stocktickerwidget.AppWidget$UpdateService; no empty constructor

我得到上述錯誤,

我不明白爲什麼。

謝謝

public class UpdateService extends Service { 
    public UpdateService() { 
     // 
     } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.e("", "onStartCommand di AppWidget"); 
     int[] appWidgetIds = intent.getIntArrayExtra("widgetsids"); 
     final int N = appWidgetIds.length; 
     AppWidgetManager manager = AppWidgetManager.getInstance(this); 

     // Perform this loop procedure for each App Widget that belongs to 
     // this provider 

     for (int i = 0; i < N; i++) { 
      int appWidgetId = appWidgetIds[i]; 
      Log.e("", 
        "i=" + Integer.toString(i) + " di " 
          + Integer.toString(N)); 
      RemoteViews view = buildUpdate(getApplicationContext(), 
        appWidgetIds); 

      // Tell the AppWidgetManager to perform an update on the current 
      // app widget 
      manager.updateAppWidget(appWidgetId, view); 

     } 
     return (START_NOT_STICKY); 

    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

回答

9

首先,刪除你的構造函數,當你從Service繼承一個公共零參數的構造函數。

然後,或者使其成爲static內部類,或者使其成爲獨立的公共Java類。您無法在此處使用常規內部類,因爲Android無法創建該內部類的實例。

+2

'static'內部類也應該是'public' – 2014-10-16 14:20:36

相關問題