Material-dialogs: Null Context is passed in from Fragment

Created on 28 Jun 2017  路  6Comments  路  Source: afollestad/material-dialogs

Firebase reports me crashes on multiple devices with API >= 23 with this Stacktrace:

Exception java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.getColor(int)' on a null object reference
android.support.v4.content.ContextCompatApi23.getColor (ContextCompatApi23.java:32)
android.support.v4.content.ContextCompat.getColor (ContextCompat.java:432)
com.afollestad.materialdialogs.util.DialogUtils.getColor (DialogUtils.java:126)
com.afollestad.materialdialogs.MaterialDialog$Builder.<init> (MaterialDialog.java:1180)
jlelse.newscatchr.ui.fragments.SettingsFragment.onPreferenceClick (SettingsFragment.kt:161)
android.support.v7.preference.Preference.performClick (Preference.java:982)
android.support.v7.preference.Preference.performClick (Preference.java:965)
android.support.v7.preference.Preference$1.onClick (Preference.java:149)
android.view.View.performClick (View.java:5685)
android.view.View$PerformClick.run (View.java:22481)
android.os.Handler.handleCallback (Handler.java:751)
android.os.Handler.dispatchMessage (Handler.java:95)

I guess there's an issue somewhere in your code. I just use this code (Kotlin) to show a simple dialog:

MaterialDialog.Builder(context)
                        .title(R.string.used_libraries)
                        .content(html.toHtml())
                        .positiveText(android.R.string.ok)
                        .build()
                        .apply {
                            contentView?.applyLinks(false)
                            show()
                        }
questiodiscussion

Most helpful comment

Are you 100% sure the context you're using isn't null at the time this is called?

I did some googling, and I found another project where someone made a similar issue - https://github.com/mozilla-mobile/focus-android/issues/73

His solution was, inside a fragment, to use a view's context instead of calling getActivity(). Perhaps that can help here.

All 6 comments

I was about to open an issue with this problem. I'm getting crash reports through Crashlytics with almost the same stack trace.

Device where it happened: Galaxy Tab3 Lite 7.0 (SM-G530H)
Android version: 4.4.4 (API 19)

Stacktrace

Fatal Exception: java.lang.NullPointerException
       at com.afollestad.materialdialogs.util.DialogUtils.getColor(DialogUtils.java:126)
       at com.afollestad.materialdialogs.MaterialDialog$Builder.<init>(MaterialDialog.java:491)
       at br.com.pentagrama.vuptonline.fragments.NotificationsFragment.getNotifications(NotificationsFragment.java:134)
       at br.com.pentagrama.vuptonline.fragments.NotificationsFragment.access$100(NotificationsFragment.java:41)
       at br.com.pentagrama.vuptonline.fragments.NotificationsFragment$2.onClick(NotificationsFragment.java:235)
       at android.support.design.widget.Snackbar$1.onClick(Snackbar.java:250)
       at android.view.View.performClick(View.java:4496)
       at android.view.View$PerformClick.run(View.java:18603)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5426)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
       at dalvik.system.NativeStart.main(NativeStart.java)

The code

loadingDialog = new MaterialDialog.Builder(getActivity())
                .cancelable(false)
                .content(R.string.activity_main_progress_dialog)
                .progress(true, 0)
                .show();

Are you 100% sure the context you're using isn't null at the time this is called?

I did some googling, and I found another project where someone made a similar issue - https://github.com/mozilla-mobile/focus-android/issues/73

His solution was, inside a fragment, to use a view's context instead of calling getActivity(). Perhaps that can help here.

I'm not 100% sure of that. Is it possible for an attached fragment to return null on getActivity()? As for OP, we can't be sure on which context he's using.

Any progress on this issue?

@jlelse your log at the top indicates you are passing a null context into MaterialDialog.Builder's constructor.

Ok...Thank You....Silly MIstake from me..

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost1372 picture ghost1372  路  5Comments

jahirfiquitiva picture jahirfiquitiva  路  6Comments

enricocid picture enricocid  路  4Comments

eygraber picture eygraber  路  3Comments

bahmandamia6982 picture bahmandamia6982  路  6Comments