简体   繁体   English

无法在 Android 中启动服务

[英]Unable to start service in Android

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 context object is probably null.您的context object 可能是 null。 Since you are in a service, which extends Context , you may as well use this.getPackageName() or just getPackageName() instead.由于您处于扩展Context的服务中,因此您也可以使用this.getPackageName()或仅使用getPackageName()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM