使用静态DatabaseHelper NullPointerException异常崩溃报告 [英] NullPointerException crash reports using static DatabaseHelper

查看:154
本文介绍了使用静态DatabaseHelper NullPointerException异常崩溃报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到来自谷歌随机NullPointerException异常崩溃报告不能被复制。我使用的是静态引用扩展OrmLiteSqliteOpenHelper一类。助手被初始化在扩展SherlockFragmentActivity主类。

I’m receiving random NullPointerException crash reports from Google that can’t be reproduced. I’m using a static reference to a class that extends OrmLiteSqliteOpenHelper. The helper is initialised in the main class that extends SherlockFragmentActivity.

if (DatabaseHelper.getInstance() == null) {
    DatabaseHelper.setInstance(this.getApplicationContext());
}

目前虽然当该装置被旋转或发送到背景可能出现它首先我。但是,没有检测量已经能够重现问题。

At first I though it may occur when the device is rotated or sent to the background. But no amount of testing has been able to recreate the issue.

无处在code是静态变量mHelper设置为null。

Nowhere in the code is the static mHelper variable set to null

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
    …
    private static DatabaseHelper mHelper;
    …
    public static DatabaseHelper getInstance() {

        return DatabaseHelper.mHelper;
    }

    public static void setInstance(Context context) {

        DatabaseHelper.mHelper = new DatabaseHelper(context);
    }
    …
}

有没有人有什么可能会导致此任何想法?

Does anyone have any idea of what may be causing this?

如下的报告之一:

java.lang.RuntimeException: Unable to start activity ComponentInfo{au.com.dcbs.tradies/au.com.dcbs.tradies.presentation.document.DocumentActivity}: java.lang.NullPointerException
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2083)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4697)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at bd.a(Document.java:654)
at au.com.dcbs.tradies.presentation.document.DocumentActivity$a.b(DocumentActivity.java:102)
at au.com.dcbs.tradies.presentation.document.DocumentActivity$a.onActivityCreated(DocumentActivity.java:78)
at e.a(FragmentManager.java:891)
at e.a(FragmentManager.java:1080)
at e.a(FragmentManager.java:1062)
at e.k(FragmentManager.java:1810)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:501)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
at android.app.Activity.performStart(Activity.java:4549)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2022)
... 11 more

线654是在该方法的第一行:

line 654 is the first line in this method:

public static Document get(long id, Context context) {

    DocumentEntity entity = DatabaseHelper.getInstance().getDocumentDao().queryForId((int)id);
    Document doc = new Document(entity, context);
    return doc;
}

getDocumentDao返回类型的对象 com.j256.ormlite.dao.RuntimeExceptionDao

推荐答案

这将如果您发布code发生问题帮助

It would help if you post the code where the problem occurs

at bd.a(Document.java:654)

不过,因为这可能会解决您的问题一般建议,可以考虑初始化数据库助手在应用类没有任何活动,因为它似乎是一个用于什么为整个应用程序,并且不依赖于特定活动的上下文

But as general suggestion that might resolve your problem, you can consider initializing the database helper in an application class not in any activity, since it seems that it is something used for the entire application and does not depend on specific activity context.

修改

我认为你需要打破code在行654,因为这是很难说哪个函数返回null。例如。

I think you need to break the code at line 654 because it is difficult to tell which function returns null. e.g.

DatabaseHelper helper = DatabaseHelper.getInstance();
...

这篇关于使用静态DatabaseHelper NullPointerException异常崩溃报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆