Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Getting an Exception with Context mContext = getActivity() ;

I noticed that if i define

Context mContext = getActivity() ;

inside the onCreateDialog method it works fine but if i define the mContext above that inside the class it raises exceptions wen i try to access it later on in setMessage,...
the reason isn't quite clear for me why the other works and the definition at the scope of class level breaks staff.

working

public class AlertDialogFragment extends DialogFragment {

          Context mContext = getActivity() ;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).
                 setTitle(mContext.getString(R.string.error_title)).
                 setMessage(mContext.getString(R.string.error_message)).
                 setPositiveButton(mContext.getString(R.string.error_button_ok), null);

        AlertDialog dialog = builder.create();
        return  dialog;
    }
}

Not Working

public class AlertDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Context mContext = getActivity() ;
         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).
                 setTitle(mContext.getString(R.string.error_title)).
                 setMessage(mContext.getString(R.string.error_message)).
                 setPositiveButton(mContext.getString(R.string.error_button_ok), null);

        AlertDialog dialog = builder.create();
        return  dialog;
    }
}
Eric Michaud
Eric Michaud
6,547 Points

I replicated your code and did not get an error. Is it a syntax error with a red underline in android studio or is it a runtime error. If it is a runtime error please post the error message that is shown in logcat.

02-08 14:05:45.935 7139-7139/com.startaddis.sunnyaddis E/AndroidRuntimeοΉ• FATAL EXCEPTION: main Process: com.startaddis.sunnyaddis, PID: 7139 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:154) at android.app.AlertDialog$Builder.<init>(AlertDialog.java:379) at com.startaddis.sunnyaddis.AlertDialogFragment.onCreateDialog(AlertDialogFragment.java:18) at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:912) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115) at android.app.BackStackRecord.run(BackStackRecord.java:833) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1580) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:451) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:155) at android.app.ActivityThread.main(ActivityThread.java:5696) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

that is the error. and also one thing to note. make sure you dint have the IF block in you main activity that we did in the next stage that checks for network availability. If you have the IF statement the code won't get there.

Eric Michaud

Eric Michaud
Eric Michaud
6,547 Points

Okay so for some reason your context is null when your dialog is being created. When I recreated your code I was able to get the dialog to show up without errors with the original code and the edited code. Honestly I'm stumped and I think it is most likely the problem is originating where the AlertDialogFragment is being instantiated elsewhere in your code.

1 Answer

Wesley Seago
Wesley Seago
10,424 Points

By moving your member variable into a method, you are changing it's scope. Somewhere in your code, as stated above, in a Dialog Builder you are referencing the context variable, but it is not available when you moved it into the method.