1

I have created a program, with a service - the service is "START_STICKY". So when I close the program (I am using Advanced Task Killer, to kill programs) the service is restarted automatically, but this presents a problem because I get an error when trying to call:

ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) (see stacktrace below).

I have created a class to handle context, which will return my Activity's context if it is not null, or a fake one I created if the activity's context is null - and the activity's context is null so the fake one is used, I suspect this may be the problem, but have no idea. In my activity I have a constructor which gives a reference of it self to the contexthandling class, but it seems the activity is not instantiated (not before the service is started at least) when the system is restarting my service.

There is of course no problem when the program is just started normally, the service starts fine then; which is why I am suspecting the fake context, I created it as follows (and it is referenced in the manifest application tag with android:name=FakeContext):

public class FakeContext extends android.app.Application 
{
   private static FakeContext m_instance = null;

   public FakeContext()
   {
   }

   public static synchronized Context getContext()
   {
      if(m_instance == null)
      {
         m_instance = new FakeContext();
      }

      return m_instance;
   }
}

Stacktrace:

11-18 19:12:14.048: ERROR/AndroidRuntime(15655): FATAL EXCEPTION: main
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): java.lang.RuntimeException: Unable to create service sdo.GeoTracker.Services.DataService: java.lang.NullPointerException
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at android.app.ActivityThread.handleCreateService(ActivityThread.java:3140)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at android.app.ActivityThread.access$3300(ActivityThread.java:135)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2202)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at android.os.Looper.loop(Looper.java:144)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at android.app.ActivityThread.main(ActivityThread.java:4937)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at java.lang.reflect.Method.invoke(Method.java:521)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at dalvik.system.NativeStart.main(Native Method)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): Caused by: java.lang.NullPointerException
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at sdo.GeoTracker.DataAccess.SqlAdapter.<init>(SqlAdapter.java:51)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at sdo.GeoTracker.DataAccess.SqlAdapter.getInstance(SqlAdapter.java:61)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at sdo.GeoTracker.Services.DataService.startService(DataService.java:65)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at sdo.GeoTracker.Services.DataService.onCreate(DataService.java:40)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): at android.app.ActivityThread.handleCreateService(ActivityThread.java:3125)
11-18 19:12:14.048: ERROR/AndroidRuntime(15655): ... 10 more

Any help is greatly appreciated! - I am a beginner at android development, so please spell it out.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SimonDK
  • 97
  • 1
  • 1
  • 7
  • why do you have a fake context ? – apps Nov 18 '10 at 21:00
  • hello, i needed to feed the sqlliteopenhelper a context. like this: private static class OpenHelper extends SQLiteOpenHelper { public OpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } ..snip.. – SimonDK Nov 18 '10 at 21:02
  • http://stackoverflow.com/questions/4177731/android-how-to-call-method-from-another-class-without-passing-context/4177776#4177776 – apps Nov 18 '10 at 21:05

1 Answers1

1

The Application class is part of the Android framework, you can't just create it with a constructor and so on. Use the Application object of your app, instead of using a "fake" one

apps
  • 942
  • 5
  • 8
  • Hello, but how do i get a hold of the application object ? i tried saving a static version of my activity class (i dont have one extending Application), and the activity class was not instantiated before the service is started. – SimonDK Nov 18 '10 at 21:06
  • pass it the Application, it is still a Context – apps Nov 18 '10 at 21:08