0

Since onAttach(Activity) has been deprecated i have trouble running this code:

public void onClick(View v) {
    if (v == loginButton){
        application.getAuth().getUser().setIsLogedIn(true);
        callbacks.onLoggedIn();
    }
}

public void onAttach(Activity activity){
    super.onAttach(activity);
    callbacks = (Callbacks) activity;
}

public interface Callbacks{
    void onLoggedIn();
}

Now I get the java.lang.NullPointerException because callbacks.onLoggedIn() returns null

I tried also:

  public void onAttach(Context context){
        super.onAttach(context);
        callbacks = (Callbacks) context.getApplicationContext();
    }
DmitryArc
  • 4,757
  • 2
  • 37
  • 42
Adrian
  • 67
  • 1
  • 14

1 Answers1

0

Here and here you can find well-reasoned discussion about your question. These posts will be helpful for better understanding of this issue.

And here are just few tips to let you quickly move on:

1) In your case you can use context as well as activity:

public void onAttach(Context context){
    super.onAttach(context);
    if(context instanceof Callbacks){
        callbacks = (Callbacks) context;
    }
}

2) After your fragment is attached you can always get parent activity with getActivity() method

Community
  • 1
  • 1
DmitryArc
  • 4,757
  • 2
  • 37
  • 42