开发者

Android SQLite ContextWrapper.openOrCreateDatabase() nullpointer?

I have created a program, with a service - the service is "START_STICKY". So when I close the开发者_Go百科 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜