Reputation:
i'm try to create simple comunication with activity and service. then after get any help from documents i'm create this service to send data from activity to service and pass data from service to activity. but i get NullPointerException for ResultReceiver
My Activity:
@Override
public void onCreate(Bundle savedInstanceState) {
resultReceiver = new MyResultReceiver(null);
txtview = (TextView) findViewById(R.id.txtview);
intent = new Intent(this, MyService.class);
intent.putExtra("receiver", resultReceiver);
startService(intent);
}
My service (with error):
public class ToobaPayamakService extends Service{
private Timer timer = new Timer();
private MyTimerTask timerTask;
private ResultReceiver resultReceiver;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
resultReceiver = intent.getParcelableExtra("mLastID");
timerTask = new MyTimerTask();
timer.scheduleAtFixedRate(timerTask, 1000, 1000);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
timer.cancel();
Bundle bundle = new Bundle();
bundle.putString("end", "Timer Stopped....");
resultReceiver.send(200, bundle);
}
class MyTimerTask extends TimerTask
{
public MyTimerTask() {
Bundle bundle = new Bundle();
bundle.putString("start", "Timer Started....");
resultReceiver.send(100, bundle);
}
@Override
public void run() {
SimpleDateFormat dateFormat = new SimpleDateFormat("s");
resultReceiver.send(Integer.parseInt(dateFormat.format(System.currentTimeMillis())), null);
}
}
}
LogCat Result:
Unable to start service com.sample.myService@41283e90 with Intent { cmp=com.sample/.myService(has extras) }: java.lang.NullPointerException
java.lang.RuntimeException: Unable to start service com.sample.myService@41283e90 with Intent { cmp=com.sample/.myService (has extras) }: java.lang.NullPointerException
error is for this line :
resultReceiver.send(100, bundle);
Upvotes: 0
Views: 590
Reputation: 3776
Not sure if this is going to be the issue but:
intent.putExtra("receiver", resultReceiver);
--------
resultReceiver = intent.getParcelableExtra("mLastID");
Your identification strings do not match, that way will never work.
Hope it helps
Upvotes: 1