我正在使用面向Android的Facebook SDK以允許用戶註冊和使用應用程序。我想獲得一些關於用戶的基本信息,如用戶名和電子郵件。無論如何,我有問題,因爲我試圖獲取用戶電子郵件時,得到空指針異常。面向Android請求電子郵件許可的Facebook SDK 3.7
String email = user.getProperty("email").toString();
我假設我得到這個錯誤,因爲我沒有請求電子郵件許可。我該怎麼做?
這裏是完整的代碼:
public class RegisterActivity extends ActionBarActivity{
Button btnFacebook;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
getSupportActionBar().hide();
btnFacebook = (Button) findViewById(R.id.btn_facebook);
btnFacebook.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("FACEBOOK", "Prije sesije");
// start Facebook Login
Session.openActiveSession(RegisterActivity.this, true, new Session.StatusCallback() {
// callback when session changes state
@SuppressWarnings("deprecation")
@Override
public void call(Session session, SessionState state,
Exception exception) {
Log.d("FACEBOOK", "U call");
if(session.isOpened()){
Log.d("FACEBOOK", "ako je sesija otvorena");
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
Log.d("FACEBOOK", "onCompleted");
if(user != null){
Log.d("FACEBOOK", "user razlicit od null");
String email = user.getProperty("email").toString();
Toast.makeText(getApplicationContext(), "Ime "+user.getUsername().toString()+ "Email "+ email ,
Toast.LENGTH_SHORT).show();
String userId = user.getId();
//new DownloadImageTask(ivProfile).execute("http://graph.facebook.com/"+userId+"/picture?type=large");
Log.d("ID", userId);
}
Log.d("ID", user.getId());
}
});
}
Log.d("FACEBOOK", "nije u sesisji");
}
});
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
是該解決方案的工作? –
該解決方案運行良好。 – Zookey
當我再添加一個權限(生日)如下,它不工作。加載RegisterActivity幾次。 –