Wildblue09
Wildblue09

Reputation: 337

Error when creating an intent inside of an AlertDialog

I am making an app that enables users to sign up on Parse.com. I have made Dialogs for the signing up just to add a more fluent feel in the app. So after they click "sign up" for example. I want it to take them to HomescreenActivity....So i made an intent inside of my Dialog. I know that It is a class and does not have context... but how would i make an intent inside of it? getContext() is not avaliable and getActivity() is giving me an error. I will post the code below. Thank you.

public class SignUpDialog extends DialogFragment {

private View v;
private LayoutInflater inflater;
private EditText adressEditText, fullnameEditText , passwordEditText , usernameEditText;



@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {




    inflater = getActivity().getLayoutInflater();
    v = inflater.inflate(R.layout.sign_up_dialog_layout , null);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v).setPositiveButton("Sign Up!" , new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            fullnameEditText = (EditText) v.findViewById(R.id.fullNameEditTextLayout);
            passwordEditText = (EditText) v.findViewById(R.id.passwordEditTextLayout);
            usernameEditText = (EditText) v.findViewById(R.id.usernameEditTextLayout);
            adressEditText = (EditText) v.findViewById(R.id.addressEditTextLayout);

            String username = usernameEditText.getText().toString().trim();
            String password = usernameEditText.getText().toString();
            String address = adressEditText.getText().toString();

            Toast.makeText(getActivity() , "We are signing you u now!" , Toast.LENGTH_SHORT).show();


            ParseUser user = new ParseUser();
            user.setUsername(username);
            user.setPassword(password);
            user.put("address", address);


            user.signUpInBackground(new SignUpCallback() {
                public void done(ParseException e) {
                    if (e == null) {
                        // Hooray! Let them use the app now.

                        Intent intent = new Intent(getActivity(), HomescreenActivity.class);
                        getActivity().startActivity(intent);



                    } else {
                        // Sign up didn't succeed. Look at the ParseException
                        // to figure out what went wrong
                    }
                }
            });





        }
    }).setNegativeButton("Cancel" , new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    return builder.create();
}

 }

Here is the error i am getting

java.lang.NullPointerException
        at com.xxx.xxx.dialogs.SignUpDialog$2$1.done(SignUpDialog.java:69)
        at com.xxx.xxx.dialogs.SignUpDialog$2$1.done(SignUpDialog.java:63)
        at com.parse.Parse$5.done(Parse.java:903)
        at com.parse.Parse$5.done(Parse.java:900)
        at com.parse.Parse$6$1.run(Parse.java:944)
        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:5017)
        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:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 207

Answers (1)

Gunnar Karlsson
Gunnar Karlsson

Reputation: 28470

In Fragments you need to null check getActivity() since the Fragment can outlive the Activity.

Alternatives to getActivity() and getContext() are:

getApplicationContext()

Or, to create a context that is available anywhere in your app, you can extend Application and create your custom application class. Inside your custom application class you create a static variable:

private static final CustomApplication INSTANCE;

In CustomApplication's onCreate():

INSTANCE = this;

Create a static method

public static CustomApplication getInstance() {
  return INSTANCE;
}

Then from anywhere in your app, to get the context, you can call:

CustomApplication.getInstance()

which is a context.

Upvotes: 3

Related Questions