[android-developers] Re: is this a bug in android OS
If you are in onCreate(), then you HAVE NOT BOUND THE SERVICE. The call to bindService() just *starts* the process of creating and binding the service. It does not complete until after you return from onCreate(), at which point the service will be actually created, it's onCreate() and onStart() methods will be called, and then, when it is finally ready, only then will your onServiceConnected() callback be called. The code that you want to run that needs to access the service can be placed in your onServiceConnected() method. onServiceConnected() is NOT CALLED from inside the bindService method -- as I outlined above, it is called later, after your onCreate method exits. Otherwise, there would be no reason to even have the onServiceConnected() method. You could just do that work right after, and the IBinder could be the return value. On Jun 25, 5:06 am, vineet wrote: > > are you calling it after you have binded ? > > YES, i have binded > > > You are mentioning onCreate(), I just hope the code-sequence is the > > Please find my code a below.. > --- > Activity Code > public class Launcher extends Activity { > private IMainService mService = null; > TextView tView1 = null; > String testvar1 = ""; > /** Called when the activity is first created. */ > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.status_display); > tView1 = (TextView)findViewById(R.id.feed_service_status); > > bindService(new Intent(IMainService.class.getName()), serCon, > Context.BIND_AUTO_CREATE); > Button refresh_button = > (Button)findViewById(R.id.refresh_button); > refresh_button.setOnClickListener(new > View.OnClickListener() { > > @Override > public void onClick(View v) { > // TODO Auto-generated method stub > fill_vals(); --->Can access from here > } > }); > fill_vals(); Cannot Access from here... > mService goes null and throw nullpointerexception. > } > > private void fill_vals() { > String tets = ""; > try { > tets = mService.ServiceStatus(); > } catch (RemoteException e) { > // TODO Auto-generated catch block > e.printStackTrace(); > tets = e.getMessage(); > } > } > > public ServiceConnection serCon = new ServiceConnection() { > > @Override > public void onServiceDisconnected(ComponentName name) { > // TODO Auto-generated method stub > } > > @Override > public void onServiceConnected(ComponentName name, IBinder > service) { > // TODO Auto-generated method stub > mService = MainServiceImpl.asInterface(service); > > } > }; > > } > > -- > > > myServiceIntent = new Intent(this,service.class); > > this.bindService(myServiceIntent, mConnection, > > Context.BIND_AUTO_CREATE); > > I have tried setting intents all the ways, by this method, by method > as i mentioned in code, tried by calling setClassName. but i dont > think that does make difference. > > On Jun 25, 4:57 pm, MobDev wrote: > > > > > Well, > > are you calling it after you have binded ? > > You are mentioning onCreate(), I just hope the code-sequence is the > > right one, thus something like : > > > myServiceIntent = new Intent(this,service.class); > > this.bindService(myServiceIntent, mConnection, > > Context.BIND_AUTO_CREATE); > > > On 25 jun, 11:40, vineet wrote: > > > > i am usign a very simple scenario. > > > i have a remote service created and binded properly. > > > I can access service methods as described in the aidl interface but > > > only from the click listener, and can never access using it using > > > onCreate or other functions. > > > > i have tried 3 different approaches as available on books or net to > > > consume service. however result is same. > > > > for instance if i create mSercvice as my Service class variable and in > > > the interface i have method getStatus() which returns boolean. > > > > now after binding service if i call mService.getStatus() from > > > onClickListener or ServiceConnection Stub, ican access very easily > > > without a error. > > > but if i try to get from onCreate (my app fetches parameter status > > > from service), the value of mService gets null and system exits with > > > Force Close (due to nullpointexception as from log). > > > > Now can somone please help, as i dont think the value should be null > > > since the variable is declared globally. so how does the stuff goes.. > > > > One of my friend on this group told, if you dont get output all times
[android-developers] Re: is this a bug in android OS
> are you calling it after you have binded ? YES, i have binded > You are mentioning onCreate(), I just hope the code-sequence is the Please find my code a below.. --- Activity Code public class Launcher extends Activity { private IMainService mService = null; TextView tView1 = null; String testvar1 = ""; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.status_display); tView1 = (TextView)findViewById(R.id.feed_service_status); bindService(new Intent(IMainService.class.getName()), serCon, Context.BIND_AUTO_CREATE); Button refresh_button = (Button)findViewById(R.id.refresh_button); refresh_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub fill_vals(); --->Can access from here } }); fill_vals(); Cannot Access from here... mService goes null and throw nullpointerexception. } private void fill_vals() { String tets = ""; try { tets = mService.ServiceStatus(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); tets = e.getMessage(); } } public ServiceConnection serCon = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub mService = MainServiceImpl.asInterface(service); } }; } -- > myServiceIntent = new Intent(this,service.class); > this.bindService(myServiceIntent, mConnection, > Context.BIND_AUTO_CREATE); I have tried setting intents all the ways, by this method, by method as i mentioned in code, tried by calling setClassName. but i dont think that does make difference. On Jun 25, 4:57 pm, MobDev wrote: > Well, > are you calling it after you have binded ? > You are mentioning onCreate(), I just hope the code-sequence is the > right one, thus something like : > > myServiceIntent = new Intent(this,service.class); > this.bindService(myServiceIntent, mConnection, > Context.BIND_AUTO_CREATE); > > On 25 jun, 11:40, vineet wrote: > > > i am usign a very simple scenario. > > i have a remote service created and binded properly. > > I can access service methods as described in the aidl interface but > > only from the click listener, and can never access using it using > > onCreate or other functions. > > > i have tried 3 different approaches as available on books or net to > > consume service. however result is same. > > > for instance if i create mSercvice as my Service class variable and in > > the interface i have method getStatus() which returns boolean. > > > now after binding service if i call mService.getStatus() from > > onClickListener or ServiceConnection Stub, ican access very easily > > without a error. > > but if i try to get from onCreate (my app fetches parameter status > > from service), the value of mService gets null and system exits with > > Force Close (due to nullpointexception as from log). > > > Now can somone please help, as i dont think the value should be null > > since the variable is declared globally. so how does the stuff goes.. > > > One of my friend on this group told, if you dont get output all times, > > its a bug. > > > sorry for bad english... > > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: is this a bug in android OS
Well, are you calling it after you have binded ? You are mentioning onCreate(), I just hope the code-sequence is the right one, thus something like : myServiceIntent = new Intent(this,service.class); this.bindService(myServiceIntent, mConnection, Context.BIND_AUTO_CREATE); On 25 jun, 11:40, vineet wrote: > i am usign a very simple scenario. > i have a remote service created and binded properly. > I can access service methods as described in the aidl interface but > only from the click listener, and can never access using it using > onCreate or other functions. > > i have tried 3 different approaches as available on books or net to > consume service. however result is same. > > for instance if i create mSercvice as my Service class variable and in > the interface i have method getStatus() which returns boolean. > > now after binding service if i call mService.getStatus() from > onClickListener or ServiceConnection Stub, ican access very easily > without a error. > but if i try to get from onCreate (my app fetches parameter status > from service), the value of mService gets null and system exits with > Force Close (due to nullpointexception as from log). > > Now can somone please help, as i dont think the value should be null > since the variable is declared globally. so how does the stuff goes.. > > One of my friend on this group told, if you dont get output all times, > its a bug. > > sorry for bad english... -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en