Starting / binding to a service is asynchronous. You can't call bindService and expect it to be already started and bound by the next line.
Call bindService and return control to Android by returning from onCreate or whatever. Your service connection callback will be invoked a little later, once the service is started. -- Kostya Vasilyev -- http://kmansoft.wordpress.com 06.08.2010 0:49 пользователь "Bender" <abende...@googlemail.com> написал: Thanks for your reply, and sorry for my late answer. :-) I tried to get it running as a service but I don't really get how I have to use services, binders and service connections. I'm reading a book with an example for services but can't adopt it to my problem. What I tried is the following: I created one class for the service, which holds the variable for my database: __________________ public class DatabaseService extends Service { public DbAdapter mDbAdapter; private DatabaseBinder mDatabaseBinder = new DatabaseBinder(); @Override public IBinder onBind(Intent intent) { return mDatabaseBinder; } @Override public void onCreate() { super.onCreate(); mDatabaseBinder.mDatabaseService = this; mDbAdapter = new DbAdapter(getApplicationContext()); mDbAdapter.open(); } @Override public void onDestroy() { super.onDestroy(); mDatabaseBinder.mDatabaseService = null; mDbAdapter.close(); } } This is my database binder: __________________ public class DatabaseBinder extends Binder { public DatabaseService mDatabaseService; public DbAdapter getDbAdapter() { return mDatabaseService.mDbAdapter; } } And this my service connection: __________________ public class DbServiceConnection implements ServiceConnection { DatabaseBinder mBinder; public DbServiceConnection(DatabaseBinder binder) { mBinder = binder; } @Override public void onServiceConnected(ComponentName className, IBinder binder) { mBinder = (DatabaseBinder) binder; } @Override public void onServiceDisconnected(ComponentName arg0) { } } If I want to use this in my activity with this: private DatabaseBinder mDatabaseBinder; private DbServiceConnection mServiceConnection = new DbServiceConnection(mDatabaseBinder); final Intent databaseServiceIntent = new Intent(this, DatabaseService.class); this.bindService(databaseServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE); mDb = mDatabaseBinder.getDbAdapter(); I'm getting a nullpointer exception at the last line. I don't know if I'm using it right (I guess not :D ), I haven't used services before. Do you know why it is throwing a Nullpointer exception? Is this the right way to use a service and bind it in the activity or should I do it somehow different? On 19 Jul., 00:30, brucko <geoff.bruck...@gmail.com> wrote: > Bender, > > put your db in a local Se... > http://developer.android.com/resources/samples/ApiDemos/src/com/examp... > > but DONT have your binder as a non-static inner class as in the > example - or you will create a... ATTENTION: Android-Beginners will be permanently disabled on August 9 2010. For more information about this change, please read [http://goo.gl/xkfl] or visit the Group home page. Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged... -- You received this message because you are subscribed to the Google Groups "Android Beginners" group. ATTENTION: Android-Beginners will be permanently disabled on August 9 2010. For more information about this change, please read [http://goo.gl/xkfl] or visit the Group home page. Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en