Question

I'm trying to communicate with a service. I found this Android Guide.

I did like in the first example but i have an error:

"java.lang.RuntimeException: Unable to bind to service com.example.internetcall.MyService@41763970 with Intent { cmp=com.example.internetcall/.MyService }: java.lang.NullPointerException: println needs a message".

This is the Activity:

public class StartService extends Activity{

String telephoneNumber;
TextView statusTextView, numberTextView;
MyService myService;
Boolean myBound;

ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName arg0, IBinder arg1) {
        Log.i("bnf","qui");
        LocalBinder binder = (LocalBinder) arg1;
        myService = binder.getService();
        myBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        myBound = false;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.start_service_layout);

    statusTextView = (TextView) this.findViewById(R.id.statusTextView);
    numberTextView = (TextView) this.findViewById(R.id.numberTextView);

    Intent i = this.getIntent();
    telephoneNumber = i.getStringExtra("number");

    numberTextView.setText(telephoneNumber);

    if(isMyServiceRunning()){
        statusTextView.setText("Online");
        statusTextView.setTextColor(Color.GREEN);
    }else{
        statusTextView.setText("Offline");
        statusTextView.setTextColor(Color.RED);
    }

    Intent intent = new Intent(this, MyService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

    //SupportClass.myService.setNumber(telephoneNumber);

}

private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if ("com.example.internetcall.MyService".equals(service.service.getClassName().toString())) {
             return true;
        }
    }
    return false;
}



}

and this is MyService:

public class MyService extends Service{

MyWebSocket mws;
private final IBinder mBinder = new LocalBinder();
Boolean onCall;
String telephoneNumber;
String myTelephoneNumber = null;

@Override
public IBinder onBind(Intent arg0) {
    Log.i("bnf",arg0.getStringExtra("number"));
    return mBinder;
}

public class LocalBinder extends Binder {
    MyService getService() {
        Log.i("bnf","localbinder");
        // Return this instance of LocalService so clients can call public methods
        return MyService.this;
    }
}

Does someone know a solution for this problem?

if i did some grammar error i sorry but i don't know english very well.

Was it helpful?

Solution

Your exception occurs at Log.i("bnf",arg0.getStringExtra("number")); arg0.getStringExtra("number") is null. You did not pass the string "number" in your intent to bind the service.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top