Facebook button fragment not opening and terminating the app - android

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);
}
}

you are not initializing mTokenTracker in your onCreate()
mTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
updateWithToken(newToken);
}
};
mTokenTracker.startTracking();
also your mProfileTracker is not initialized
mProfileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(com.facebook.Profile profile_old, com.facebook.Profile profile_new) {
// profile2 is the new profile
profile = profile_new;
mProfileTracker.stopTracking();
}
};
mProfileTracker.startTracking();

Related

Android Facebook SDK | How to get day of birth

I managed to get the first_name, last_name and the link_uri. I am not sure how to get the user's date of birth.
This is the code I used in my loginActivity:
public class LoginActivity extends AppCompatActivity {
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
nextActivity(newProfile);
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
nextActivity(profile);
Toast.makeText(getApplicationContext(), "Logging in...", Toast.LENGTH_SHORT).show();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
loginButton.setReadPermissions("user_friends");
loginButton.registerCallback(callbackManager, callback);
}
#Override
protected void onResume() {
super.onResume();
//Facebook login
Profile profile = Profile.getCurrentProfile();
nextActivity(profile);
}
#Override
protected void onPause() {
super.onPause();
}
protected void onStop() {
super.onStop();
//Facebook login
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
#Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
super.onActivityResult(requestCode, responseCode, intent);
//Facebook login
callbackManager.onActivityResult(requestCode, responseCode, intent);
}
private void nextActivity(Profile profile) {
if (profile != null) {
Intent main = new Intent(LoginActivity.this, MainActivity.class);
main.putExtra("name", profile.getFirstName());
main.putExtra("surname", profile.getLastName());
main.putExtra("imageUrl", profile.getProfilePictureUri(200, 200).toString());
startActivity(main);
}
}
}
I want to show the age of the user in the next activity so do I also need to do something with the returned value? (I assume it would be something like dd/mm/yyyy or mm/dd/yyyy)
you have to submit you application to review on facebook developer console or
create test developer for the app. and for those account ask for permission of "date of birth" while running code, you will get dob for those test developer

Get Facebook Username on MainActivity from 3rd Activity

I have 3 Activity's.
MainActivity has the side_nav_menu (Later I want the circle picture).
FacebookActivity has the facebook SDK and and the Fragment.
BlankFragment has all the facebook login with a txtView that gets the current user name.
Whats the best way to get the user name, picture and email. So I can show it on side_nav_menu.
How this callbackManager works?
FacebookActivity extends to Fragment
private BlankFragment mainFragment;
private LoginButton loginButton;
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facebook);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
printHashkey();
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new BlankFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, mainFragment).commit();
} else {
// Or set the fragment from restored state info
mainFragment = (BlankFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
}
public void printHashkey(){
try {
PackageInfo info = getPackageManager().getPackageInfo(
"BLA BLA BLA BLA BLA BLA",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
And the Fragment
private CallbackManager callbackManager;
private TextView textView;
private ImageView imv;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
displayMessage(profile);
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
public BlankFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
accessTokenTracker= new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
displayMessage(newProfile);
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
textView = (TextView) view.findViewById(R.id.textView);
loginButton.setReadPermissions("public_profile");
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, callback);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private void displayMessage(Profile profile){
TextView textView = (TextView) getActivity().findViewById(R.id.textView);
ProfilePictureView profileImage = (ProfilePictureView) getActivity().findViewById(R.id.profilePicture);
if(profile != null){
textView.setText(profile.getName());
profileImage.setProfileId(profile.getId());
}
}
#Override
public void onStop() {
super.onStop();
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
#Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
displayMessage(profile);
}
How can I get user Name, and Picture. Send to MainActivity on the side_nav_menu?
This part here show the pic and the name on the fragment.
private void displayMessage(Profile profile){
TextView textView = (TextView) getActivity().findViewById(R.id.textView);
ProfilePictureView profileImage = (ProfilePictureView) getActivity().findViewById(R.id.profilePicture);
profileImage.setProfileId(profile.getId());
if(profile != null){
textView.setText(profile.getName());
profileImage.setProfileId(profile.getId());
}
}
You should probably save it in SharedPreferences (for image just save path so you can load it from sdcard, or just save image url)

Unable to get email from the GraphRequest class of the Facebook

Here is my fbLoginFragment.java in which I am trying to get the user logged in using Facebook SDK 4.9.0 -:
LoginButton loginButton;
AccessToken accessToken;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_fblogin, container, false);
}
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Application code
String email = object.optString("email");
Log.i("Radhe", response.toString());
Log.i("Radhe", object.toString() + " " +email);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
Log.i("Radhe", "Cancelled");
}
#Override
public void onError(FacebookException e) {
Log.i("Radhe", "Error = " + e);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
Log.i("Radhe", "So Hari it is coming here");
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile");
loginButton.setReadPermissions("email");
loginButton.setReadPermissions("user_birthday");
loginButton.setReadPermissions("user_friends");
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, callback);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onStop() {
super.onStop();
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
#Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
displayMessage(profile);
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
private void displayMessage(Profile profile) {
/****************************Save user to Parse***********************************/
if (profile != null) {
ParseUser user = new ParseUser();
user.setUsername(profile.getId().toString());
user.setPassword(profile.getId().toString());
user.put("legalname", profile.getFirstName());
user.put("surname", profile.getLastName());
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
Log.i("Radhe", "Radhe! Parse signup is success");
startActivity(new Intent(getActivity().getApplicationContext(), home.class));
} else {
e.printStackTrace();
Log.i("Radhe", "Radhe! Parse signup is failure " + e);
}
}
});
}
}
I have given the permissions also but I am not getting email and date of birth in the JSON object.
Here is the result of my Logcat-:
01-22 16:29:07.909 8841-8841/? I/Radhe: {Response: responseCode: 200, graphObject: {"id":"820969881382796","gender":"male","name":"Pranav Shukla"}, error: null}
01-22 16:29:07.909 8841-8841/? I/Radhe: {"id":"820969881382796","gender":"male","name":"Pranav Shukla"}
You need to add permission as List like below:
....
List<String> permissions = new ArrayList<String>();
permissions.add("public_profile");
permissions.add("email");
permissions.add("user_birthday");
permissions.add("user_friends");
loginButton.setReadPermissions(permissions);
....
Check Facebook doc : public void setReadPermissions(List permissions)

FaceBook SDK for android login not working

so i created a simple facebook login app following the exact instructions from this video: https://www.youtube.com/watch?v=myWu-q8Q2NA&list=PLonJJ3BVjZW4E1wIRZvuXhbFRGWB1grmh
anyways i am able to sign in but unlike the video, when I sign in, I appear back at the login page, the login button does not change to the logout button,
I click the login button, enter my login details, hit send, then a message says: You have already authorized "LoginExample (app name)". Then I appear back at the login with facebook button.
and on a similar note, I am trying to get a TextView to display a "Welcome", however it does not work, it only works when I do ' android:text="Welcome" '.
Below is my code in a fragment, I do setContentView(activity_fragment); in MainActivity. Again, the Login button appears but the TextView does not.
What is going wrong?
public class MainFragment extends Fragment {
public TextView mTextDetails;
private AccessTokenTracker mtokentracker;
private ProfileTracker mprofiletracker;
CallbackManager mCallBackManager;
private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
//TextView mTextDetails = (TextView)getView().findViewById(R.id.textView);
Profile profile = Profile.getCurrentProfile();
if (profile != null){
mTextDetails.setText("Welcome " + profile.getName());
}
//setContentView(R.layout.activity_main);
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
public MainFragment() {
//TextView mTextDetails = (TextView) getView().findViewById(R.id.textView);
//mTextDetails.setText("Welcome");
}
#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 old, AccessToken niw) {
Profile profile = Profile.getCurrentProfile();
if (profile != null){
mTextDetails.setText("welcome " + profile.getName());
}
}
};
ProfileTracker profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile old, Profile niw) {
}
};
mtokentracker.startTracking();
mprofiletracker.startTracking();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onViewCreated(View view, 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);
TextView mTextDetails = (TextView)getView().findViewById(R.id.textView);
mTextDetails.setText("Welcome");
}
#Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
if (profile != null){
mTextDetails.setText("Welcome " + profile.getName());
}
}
#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);
}
}

I dont know how using LOGOUT in FACEBOOK API ANDROID

I have the follow code, but I don't know which is the method that execute when I push in Logout. I only need know which is the method that execute in logout. Or another solution? Thanks for all :) dasasa
public class FBLoginFragment extends Fragment implements IServiceCallback {
private CallbackManager callbackManager;
private TextView textView;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker mProfileTracker;
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
mProfileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
mProfileTracker.stopTracking();
}
};
mProfileTracker.startTracking();
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
displayMessage(profile);
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
public FBLoginFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
accessTokenTracker= new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
mProfileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
displayMessage(newProfile);
if(newProfile != null){
register(newProfile);
}
}
};
accessTokenTracker.startTracking();
mProfileTracker.startTracking();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.facebook_fragment, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
textView = (TextView) view.findViewById(R.id.textView);
loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, callback);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (callbackManager.onActivityResult(requestCode, resultCode, data))
return;
}
private void displayMessage(Profile profile){
if(profile != null){
textView.setText(profile.getName());
}
}
private void register(Profile profile){
if(profile != null){
new Register(this).run(profile);
}
}
#Override
public void onStop() {
super.onStop();
accessTokenTracker.stopTracking();
mProfileTracker.stopTracking();
}
#Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
displayMessage(profile);
}
#Override
public void onSuccess(JSONObject obj, String method) throws JSONException {
User user = new User(obj);
user.insert();
//Intent intent = new Intent(getActivity(), Update.class);
//startActivity(intent);
}
#Override
public void onError(String error) {
}
You need to get the active Session, then then call closeAndClearTokenInformation().
You can get the currently active session via Session.getActiveSession().
Try this method to logout from facebook programmatically in android
/**
* Logout From Facebook
*/
public static void callFacebookLogout(Context context) {
Session session = Session.getActiveSession();
if (session != null) {
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
} else {
session = new Session(context);
Session.setActiveSession(session);
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
}

Categories

Resources