I want to obtain the public profile data {email, id and profile picture}, but when I start my application it crashes, then I will leave my code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if(AccessToken.getCurrentAccessToken()==null){
String data = getIntent().getExtras().getString("email");
email=data;
NavigationView navigationsView = (NavigationView) findViewById(R.id.nav_view);
View hView = navigationsView.getHeaderView(0);
TextView nav_user = (TextView)hView.findViewById(R.id.txtMail);
nav_user.setText(email);
}
else{
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
try {
user.setEmail(response.getJSONObject().get("email").toString());
user.setName(response.getJSONObject().get("name").toString());
user.setImageUri(response.getJSONObject().get("picture").toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,email");
request.setParameters(parameters);
request.executeAsync();
String correos=user.getEmail();
NavigationView navigationsView = (NavigationView) findViewById(R.id.nav_view);
View hView = navigationsView.getHeaderView(0);
TextView nav_user = (TextView)hView.findViewById(R.id.txtMail);
nav_user.setText(correos);
ImageView fperfil=(ImageView) hView.findViewById(R.id.imageView);
fperfil.setImageResource(Integer.parseInt(user.getImageUri()));
}
and this is my logcat
11-03 16:15:11.755 30076-30076/com.ciclomapp.ciclomapp.ciclomapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ciclomapp.ciclomapp.ciclomapp, PID: 30076
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ciclomapp.ciclomapp.ciclomapp/com.ciclomapp.ciclomapp.ciclomapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.facebook.AccessToken com.facebook.GraphRequest.getAccessToken()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.facebook.AccessToken com.facebook.GraphRequest.getAccessToken()' on a null object reference
at com.ciclomapp.ciclomapp.ciclomapp.MainActivity.onCreate(MainActivity.java:103)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
You are not considering several steps like for example the existence of the keys... and your key "picture" does not exist in FB 4.5
This should help you get the profile picture, first name and last name