I think i did everything well, but i don't know why this appear
This MainActivity.java Code
package com.smarty_tech.boundservice;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static String LOG_TAG = "MainActivity";
private Button btnStartService;
private Button btnStopService;
SmartyService service;
boolean mBound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent intent = new Intent(MainActivity.this, SmartyService.class);
btnStartService = (Button) findViewById(R.id.btn_start_service);
btnStopService = (Button) findViewById(R.id.btn_stop_service);
btnStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bindService(intent, connection, Service.BIND_AUTO_CREATE);
int x = service.getRandomNumber();
Log.e("Start Bound Service", "Bound Successfuly and X value is ");
}
});
btnStopService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
unbindService(connection);
Log.e("Start Bound Service", "UnBounded Successfuly");
}
});
}
public ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
SmartyService.SmartyBinder smartyBinder = (SmartyService.SmartyBinder) service;
MainActivity.this.service = smartyBinder.getService();
mBound = true;
Log.e(LOG_TAG, "onServiceConnected call");
}
@Override
public void onServiceDisconnected(ComponentName className) {
MainActivity.this.service = null;
mBound = false;
Log.e(LOG_TAG, "onServiceDisconnected call");
}
};
}
And this is my Service Code
package com.smarty_tech.boundservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import java.util.Random;
public class SmartyService extends Service {
private static String LOG_TAG = "SmartyService";
public SmartyService() {
}
private final IBinder binder = new SmartyBinder();
private final Random mGenerator = new Random();
public class SmartyBinder extends Binder{
SmartyService getService(){
Log.v(LOG_TAG, "in SmartyBinder");
return SmartyService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
Log.v(LOG_TAG, "in onBind");
return binder;
}
@Override
public void onRebind(Intent intent) {
Log.v(LOG_TAG, "in onRebind");
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
Log.v(LOG_TAG, "in onUnbind");
return true;
}
@Override
public void onCreate() {
super.onCreate();
Log.e(LOG_TAG, "onCreated Called !");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(LOG_TAG, "onDestroy Called , I will miss you !");
}
public void printHelloWorld(){
Log.e(LOG_TAG, "Hello Wolrd");
}
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}
and This is main_activity.xml of the layout
<pre><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_start_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service"
/>
<Button
android:id="@+id/btn_stop_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Service"
/>
</LinearLayout>
What I have tried:
When i try to start service, the following exception appears on Logcat
06-11 09:43:03.654 2380-2380/com.smarty_tech.boundservice E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smarty_tech.boundservice, PID: 2380
java.lang.NullPointerException
at com.smarty_tech.boundservice.MainActivity$1.onClick(MainActivity.java:39)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)