My main activity has three check boxes in it. I want to be able to pass whether or not these check boxes are checked to a service when the “submit” button is pressed. Here is my code:
public void onClick(View v) {
// TODO Auto-generated method stub
Intent start_service = new Intent();
start_service.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
start_service.setClass(getApplicationContext(), FollowService.class);
if( box1.isChecked() ){
boxes[0] = 1;
}
if( box2.isChecked() ){
boxes[1] = 1;
}
if( box3.isChecked() ){
boxes[2] = 1;
}
start_service.putExtra("com.mypackage.boxes", selections);
box1.setChecked(false);
box2.setChecked(false);
box3.setChecked(false);
getApplicationContext().startService(start_service);
}
I then try to access this array that I am attempting to pass to the service inside of my onStartCommand by doing the following:
public int onStartCommand(Intent intent, int flags, int startID){
try {
Bundle selections = intent.getExtras();
int [] boxes = selections.getIntArray("boxes");
if( boxes[0] == 1 ){
// do something
}
if( boxes[1] == 1 ){
// do something
}
if( boxes[1] == 1 ){ // do something
}
checkWebsite();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return START_STICKY;
}
My application will start up and doesn’t force close on me when I press the “submit” button, but I am getting a system.err message in LogCat.
Here is the LogCat error message that I have received when I try to start my service.
06-19 04:20:51.514: WARN/System.err(300): java.lang.NullPointerException
06-19 04:20:51.536: WARN/System.err(300): at com.mypackage.FollowService.onStartCommand(FollowService.java:60)
06-19 04:20:51.536: WARN/System.err(300): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
06-19 04:20:51.544: WARN/System.err(300): at android.app.ActivityThread.access$3600(ActivityThread.java:125)
06-19 04:20:51.544: WARN/System.err(300): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
06-19 04:20:51.554: WARN/System.err(300): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 04:20:51.554: WARN/System.err(300): at android.os.Looper.loop(Looper.java:123)
06-19 04:20:51.554: WARN/System.err(300): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-19 04:20:51.574: WARN/System.err(300): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 04:20:51.574: WARN/System.err(300): at java.lang.reflect.Method.invoke(Method.java:521)
06-19 04:20:51.574: WARN/System.err(300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-19 04:20:51.574: WARN/System.err(300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-19 04:20:51.584: WARN/System.err(300): at dalvik.system.NativeStart.main(Native Method)
follow below steps
1) make a class file name first.java and write below code
2) make a service name second.java and put below code
3) and menifest like this