I have created a fragment with facebook button. It worked for some times and now when I am trying to open the fragment using the navigation app drawer, it is crashing the app.
Below is the stack trace:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myclass.myapp, PID: 3526 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.AccessTokenTracker.startTracking()' on a null object reference at com.myclass.myapp.FacebookLogin.onCreate(FacebookLogin.java:82) at android.support.v4.app.Fragment.performCreate(Fragment.java:2177) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1244) at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1080) at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:971) at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2143) at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Application terminated.
Code of fragment class:
public class FacebookLogin extends Fragment {
//initialize WebView
private TextView mTextDetails;
private CallbackManager mCallbackManager;
private AccessTokenTracker mTokenTracker;
private ProfileTracker mProfileTracker;
private FacebookCallback<LoginResult> mCallback=new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
displayWelcomeMessage(profile);
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallbackManager=CallbackManager.Factory.create();
AccessTokenTracker tracker=new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
}
};
ProfileTracker profileTracker=new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
displayWelcomeMessage(currentProfile);
}
};
mTokenTracker.startTracking();
mProfileTracker.startTracking();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.facebook_login, container, false);
}
private void displayWelcomeMessage(Profile profile){
if (profile != null){
mTextDetails.setText(profile.getName());
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
loginButton.setFragment(this);
loginButton.registerCallback(mCallbackManager,mCallback);
mTextDetails = (TextView)view.findViewById(R.id.text_details);
}
@Override
public void onResume() {
super.onResume();
Profile profile=Profile.getCurrentProfile();
displayWelcomeMessage(profile);
}
@Override
public void onStop(){
super.onStop();
mTokenTracker.stopTracking();
mProfileTracker.stopTracking();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode,resultCode, data);
}
}