Reputation: 1252
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)
Upvotes: 1
Views: 2359
Reputation: 33996
follow below steps
1) make a class file name first.java and write below code
package com.example.bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class first extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(this, second.class);
int[] arr = new int[3];
arr[0]=0;
arr[1]=1;
arr[2]=2;
Bundle bundle = new Bundle();
bundle.putIntArray("arr", arr);
intent.putExtras(bundle);
startService(intent);
}
}
2) make a service name second.java and put below code
package com.example.bundle;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;
public class second extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras();
int[] arr = bundle.getIntArray("arr");
Toast.makeText(second.this, "" + arr[0] + " " + arr[1] + " " + arr[2], Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}
}
3) and menifest like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bundle"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".first" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".second" android:label="@string/app_name">
</service>
</application>
</manifest>
Upvotes: 2
Reputation: 6182
You might need to do:
selections.getIntArray("com.mypackage.boxes")
I might be wrong, but adding your package name is just a good standard to follow, not a requirement.
Upvotes: 0