I made I widget for reading news-items. If I put one widget at my homescreen, everything is fine. But if I put a seconde one at my homescreen and I remove it, the service of the widget stops. So if the widget gets in the update methode, an error occurs.
java.lang.RuntimeException: Unable to start service news.WidgetActivity$UpdateService@405231d8 with Intent { flg=0x10000000 cmp=news/.WidgetActivity$UpdateService bnds=[26,97][79,151] }: java.lang.NullPointerException
A nullpointer is at this line:
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Here the code:
public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setViewVisibility(R.id.WidgetRefresh_btn, View.GONE);
remoteViews.setViewVisibility(R.id.progressWidget, View.VISIBLE);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
WidgetActivity.Widget.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Your
contextobject is probably null. Since you are in a service, which extendsContext, you may as well usethis.getPackageName()or justgetPackageName()instead.