2013-03-27 69 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

相关问题