Android SQLite ContextWrapper.openOrCreateDatabase() 空指针?

发布于 2024-10-03 05:27:58 字数 3458 浏览 3 评论 0原文

我创建了一个带有服务的程序 - 该服务是“START_STICKY”。因此,当我关闭程序(我正在使用高级任务杀手来终止程序)时,服务会自动重新启动,但这会出现一个问题,因为我在尝试调用时收到错误:

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

我创建了一个类来处理上下文,这将如果活动的上下文不为空,则返回我的活动的上下文,或者如果活动的上下文为空,则返回我创建的假上下文 - 并且活动的上下文为空,因此使用了假的上下文,我怀疑这可能是问题所在,但不知道。 在我的活动中,我有一个构造函数,它向 contexthandling 类提供其自身的引用,但当系统重新启动我的系统时,该活动似乎没有实例化(至少在服务启动之前没有实例化)服务。

程序刚正常启动时当然没有问题,服务启动正常;这就是为什么我怀疑假上下文,我按如下方式创建了它(并且在清单应用程序标记中使用 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

非常感谢任何帮助! - 我是android开发的初学者,所以请详细说明。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生池水覆流年 2024-10-10 05:27:58

Application类是Android框架的一部分,你不能只用构造函数等来创建它。使用应用程序的 Application 对象,而不是使用“假”对象

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文