如何修复此特定异常?

时间:2015-01-27 16:09:53

标签: android

我有与Android相关的具体问题。我正在使用Acra进行错误报告,并且我将所有错误保存在我的本地数据库中。 在我的上次应用程序更新后,出现了一个特定问题。 我每小时在我的数据库上看到近20个新错误,这是一个简单的Nullpointerexception,但是当我在不同设备上尝试我的应用程序时,它运行得很好。 我试过三星手机,Lg Nexus手机,华硕平板电脑和genymotion模拟器。每当我尝试时,场合都是一样的。我无法解决问题因为我无法在手机上看到这个问题。

我该怎么办?

我也在分享我的代码。

我的错误报告

java.lang.RuntimeException: Unable to start receiver com.tupbebekailesi.serkanoral.hamilelikrehberi.WeekAlarms: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2567)
at android.app.ActivityThread.access$1800(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.tupbebekailesi.serkanoral.hamilelikrehberi.MotherActivity.week_number(MotherActivity.java:321)
at com.tupbebekailesi.serkanoral.hamilelikrehberi.WeekAlarms.onReceive(WeekAlarms.java:41)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2552)
... 10 more
java.lang.NullPointerException
at com.tupbebekailesi.serkanoral.hamilelikrehberi.MotherActivity.week_number(MotherActivity.java:321)
at com.tupbebekailesi.serkanoral.hamilelikrehberi.WeekAlarms.onReceive(WeekAlarms.java:41)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2552)
at android.app.ActivityThread.access$1800(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)

本周编号方法

public static int week_number() {
        long get_due_date_ms = preferences.getLong( USER_DUE_DATE_MS, 0 );
        long today = System.currentTimeMillis();
        long elapsed_time = today - get_due_date_ms;
        int week_day = ( int ) ( elapsed_time / CalculateActivity.bir_hafta );

        if ( week_day > 39 || week_day < 0 )
            week_day = 0;

        return week_day;
    }

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我认为偏好可能是空的。在这种情况下,下面的代码可以解决您的问题。

public static int week_number() {
    if(preferences != null){
        long get_due_date_ms = preferences.getLong( USER_DUE_DATE_MS, 0 );
        long today = System.currentTimeMillis();
        long elapsed_time = today - get_due_date_ms;
        int week_day = ( int ) ( elapsed_time / CalculateActivity.bir_hafta);

        if ( week_day > 39 || week_day < 0 )
            week_day = 0;
        }
        return week_day;
    }
}