ServiceConnection.onServiceConnected() never called after binding to started service

喜欢而已 提交于 2019-12-01 20:19:23

ServiceConnection's onServiceConnected() is called, but nobody guarantees that it will be called before onCreate continues execution. So, what happens here - you successfuly bind to the service (that's why onBind returns true), but you're not fully connected - onServiceConnected() has not yet been called, so your local mProgressService object is not yet initalized, and therefore you get the NullPointerException.

Solution:

Move these two lines:

mProgressBarList = mProgressService.getProgressBarList();
mStaffNameList = mProgressService.getStaffNameList();

from onCreate() to onServiceConnected() function (use the service object after it is initialized in onServiceConnected()).

Check AndroidManifest.xml of yours and add service that you tried to bind.

You have to return your Binder inner class from

private final IBinder mBinder = new ServiceBinder();

 public class ServiceBinder extends Binder {
   public PlayerActivity getService() {
   return PlayerActivity.this;
     }
  }

@Nullable
@Override
           public IBinder onBind(Intent intent) {
             return mBinder;
           }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!