0

I have a started service and some activities must bind to it to get some data before set the views. Everything is working fine but, some (rarely) times, I got a NullPointerException. My simplified activitiy is:

public class MyActivity extends Activity {
    TextView tvName;
    boolean mIsMyServiceBound;
    MyService mMyService;

    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            MyService.MyServiceBinder myServiceBinder = (MyService.MyServiceBinder) service;
            mMyService = myServiceBinder();
            mIsMyServiceBound = true;

            // Set up views
            tvName.setText(mMyService.getName());
        }

        @Override
        public void onServiceDisconnected(ComponentName className) {
            mIsMyServiceBound = false;
            mMyService = null;
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.yourlayout);
        tvName = (TextView) findViewById(R.id.tv_name);
        ...
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, ChatService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        mIsChatServiceBound = true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mIsChatServiceBound) {
            unbindService(mConnection);
            mIsChatServiceBound = false;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        tvName = null;
    }
}

Well, it is usually working fine. But I've got a NullPointerException when doing:

tvName.setText(mMyService.getName());

The error tells that tvName is null, but I don't understand how it can be possible, as it will be called after onCreate. This error happens rarely times, but this is quite annoying. May the activity had been destroyed but the service connection listener didn't cancelled? If this is true, how could I cancel that service connection when the activity's destroyed?

Thanks in advance!

FVod
  • 2,245
  • 5
  • 25
  • 52
  • 1
    You are not extending `Activity` or something similiar, please don't over simplify your code you post here. You may be creating more errors than you have and we are unable to help you. – yennsarah Feb 29 '16 at 08:24
  • I've edited the code, I forgot to write it too. Sorry for the inconvenience, now I've updated the code. – FVod Feb 29 '16 at 08:26
  • Why do you set your `tvName`to null in `onDestroy()`? If you simply add a `if(tvName == null) //do something else, maybe restart the activity and else tvName.setText("")`, you won't get this crash. – yennsarah Feb 29 '16 at 08:31
  • Try to bind your ServiceConntection in `onResume()`, and unbind it maybe also in `onPause`, regarding the [activity lifecycle](http://i.stack.imgur.com/LXnx7.png). – yennsarah Feb 29 '16 at 08:36

3 Answers3

0

Try this. You are missing

setContentView(R.layout.yourlayout);

in onCreate Method.You need to extend to Activity class too Hope it helps.thanks

Jagjit Singh
  • 1,909
  • 1
  • 14
  • 19
  • Thanks, I forgot to write it on my simplified example. Sorry, I have edited it now, because the setcontentView is in my real code, otherwise it would always crash. Thank you for your answer, do you have any clue about what may be happening? – FVod Feb 29 '16 at 08:24
0

You forgot to write setcontentView(R.layout.activity_main) to Activity.

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setcontentView(R.layout.activity_main)
     tvName = (TextView) findViewById(R.id.tv_name);
}

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • Thanks, I forgot to write it on my simplified example. Sorry, I have edited it now, because the setcontentView is in my real code, otherwise it would always crash. Thank you for your answer, do you have any clue about what can be happening? – FVod Feb 29 '16 at 08:23
  • @FVod, make sure you have textview with tv_name id in your layout – Hiren Patel Feb 29 '16 at 08:26
  • Yes, it is present. In fact that error just happens rarely times. Thats why I think that there's something odd here, something like destroying the activity while the service connection listener haven't already been triggered. What do you think? – FVod Feb 29 '16 at 08:27
0

Try to see what happens if you open your activity and quickly rotate your phone many times after. If there is a problem with your Service connectivity and activity cycle you should be able to discover it this way. Also as a workaround you can try to get the reference to the TextView inside the onServiceConnected and make the sanity check. (Very weird though, first time I hear about this, it can be something related with your app architecture). Also here:

MyService.MyServiceBinder myServiceBinder = (MyService.MyServiceBinder)service; mMyService = myServiceBinder();

You should not use the Service this way, instead create a Messenger from the Binder and use a Handler on Service side to perform the required operations.