I\'m trying to log in without using login button. I followed facebook tutorial but i can not get it work, always give me NullPointerException. My Manifest:
&
Try in this way:
Create Android app on Facebook developer and set it to public instead development mode.
Manifest:
Strings.xml
XXXXXXXXXXXXXX
If you want to use fragment for Login the proper way is:
Step 1:
CallbackManager mCallbackManager;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//@Deprecated for latest version, without this line working fine.
//FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(mCallbackManager,
new FacebookCallback() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.e("Success", "Login");
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name ,email, gender, birthday");
GraphRequest gr = GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject json, GraphResponse response) {
if (response.getError() != null) {
// handle error
System.out.println("ERROR "+response.getError().toString());
} else {
//System.out.println("Success");
try {
String jsonresult = String.valueOf(json);
System.out.println("JSON Result"+jsonresult);
String resp = String.valueOf(response.getJSONObject());
System.out.println("JSON resp "+resp);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
gr.setParameters(parameters);
gr.executeAsync();
}
@Override
public void onCancel() {
// App code
Log.e("onCancel", "Login");
}
@Override
public void onError(FacebookException onError) {
// App code
Log.e("onError", "Login "+onError.getMessage());
}
});
}
Step 2:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}
Step 3: - inside OnClickListener
LoginManager.getInstance().logInWithReadPermissions(YourFragment.this, Arrays.asList("public_profile"));
I was using getActivity() and because of this every time something flashes instead showing Facebook Login view. Basically instead getActivity() have to use FragmentClaseName.this. I hope this will help u. Happy coding!!!