java.lang.RuntimeException - Caused by: java.lang.ArithmeticException

1.4k views Asked by At

Here are two play console logs and I think both are same issue.
I used https://github.com/z3r0c00l-2k/AquaDroid this library in my app which is on play store.
But I do not know how to solve this crash which is happening.
please someone help me, also posting code of shallNotify below...please help

java.lang.RuntimeException:
at android.app.ActivityThread.handleReceiver (ActivityThread.java:4114)
at android.app.ActivityThread.access$1500 (ActivityThread.java:250)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1984)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:250)
at android.app.ActivityThread.main (ActivityThread.java:7766)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:604)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:958)
Caused by: java.lang.ArithmeticException:
at myapp.w3.b.f (Unknown Source:47)
at myapp.w3.b.e (Unknown Source)
at myapp.recievers.NotifierReceiver.onReceive (Unknown Source:91)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:4105)

java.lang.RuntimeException:
at android.app.ActivityThread.handleReceiver (ActivityThread.java:3798)
at android.app.ActivityThread.access$1400 (ActivityThread.java:220)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1871)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loop (Looper.java:214)
at android.app.ActivityThread.main (ActivityThread.java:7403)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:935)
Caused by: java.lang.ArithmeticException:
at myapp.helpers.NotificationHelper.shallNotify (NotificationHelper.java:47)
at myapp.helpers.NotificationHelper.notify (NotificationHelper.java)
at myapp.recievers.NotifierReceiver.onReceive (NotifierReceiver.java:91)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:3789)
at android.app.ActivityThread.access$1400 (ActivityThread.java:220)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1871)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loop (Looper.java:214)
at android.app.ActivityThread.main (ActivityThread.java:7403)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:935)

        private fun shallNotify(): Boolean {
        val prefs = ctx.getSharedPreferences(AppUtils.USERS_SHARED_PREF, AppUtils.PRIVATE_MODE)
        val sqliteHelper = SqliteHelper(ctx)

        val percent = sqliteHelper.getIntook(AppUtils.getCurrentDate()!!) * 100 /      prefs.getInt(AppUtils.TOTAL_INTAKE, 0)

        var doNotDisturbOff = true

        val startTimestamp = prefs.getLong(AppUtils.WAKEUP_TIME, 0)
        val stopTimestamp = prefs.getLong(AppUtils.SLEEPING_TIME_KEY, 0)

        if (startTimestamp > 0 && stopTimestamp > 0) {
            val now = Calendar.getInstance().time

            val start = Date(startTimestamp)
            val stop = Date(stopTimestamp)

            doNotDisturbOff = compareTimes(now, start) >= 0 && compareTimes(now, stop) <= 0
        }

        return doNotDisturbOff && (percent < 100)
    }
        fun notify(id: Long, notification: NotificationCompat.Builder?) {
        if (shallNotify()) {
            getManager()!!.notify(id.toInt(), notification!!.build())
        } else {
            Log.i("AquaDroid", "dnd period")
        }
    }
        override fun onReceive(context: Context, intent: Intent) {

        val prefs = context.getSharedPreferences(AppUtils.USERS_SHARED_PREF, AppUtils.PRIVATE_MODE)
        val notificationsTone = prefs.getString(
            AppUtils.NOTIFICATION_TONE_URI_KEY, RingtoneManager.getDefaultUri(
                RingtoneManager.TYPE_NOTIFICATION
            ).toString()
        )

        val title = context.resources.getString(R.string.app_name)
        val messageToShow = prefs.getString(
            AppUtils.NOTIFICATION_MSG_KEY,
            context.resources.getString(R.string.pref_notification_message_value)
        )

        /* Notify */
        val nHelper = NotificationHelper(context)
        @SuppressLint("ResourceType") val nBuilder = messageToShow?.let {
            nHelper
                .getNotification(title, it, notificationsTone)
        }
        nHelper.notify(1, nBuilder)

    }
1

There are 1 answers

6
laalto On BEST ANSWER
val percent = sqliteHelper.getIntook(AppUtils.getCurrentDate()!!) * 100 /      prefs.getInt(AppUtils.TOTAL_INTAKE, 0)

You are potentially dividing by zero here. The sharedpreferences default value is 0 so one obvious way to crash here is to not have a value for TOTAL_INTAKE in prefs, or to have a zero value.