我已经为facebook集成制作了一个演示Android应用程序,我已经按照以下教程一步一步地进行,Facebook connect tutorial我成功获得了access_token,但之后我在应用程序中得到了一个nullpointer异常
我的代码
private static String APP_ID = "1464688850452016"; // Replace with your App
// ID
// Instance of Facebook Class
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
Button btn_fb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
mAsyncRunner = new AsyncFacebookRunner(facebook);
init();
btn_login.setOnClickListener(this);
}
@SuppressWarnings("deprecation")
void init() {
facebook = new Facebook(APP_ID);
new AsyncFacebookRunner(facebook);
btn_fb = (Button) findViewById(R.id.btn_fb);
btn_fb.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_fb:
Log.d("Image Button", "button Clicked");
loginToFacebook();
break;
}
}
// Login From Facebook...!!!
// Checking facebook session..!!!
public void loginToFacebook() {
facebook.authorize(this, new String[] { "email", "publish_stream" },
new DialogListener() {
@Override
public void onCancel() {
// Function to handle cancel event
}
@Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
// Making Login button invisible
}
@Override
public void onError(DialogError error) {
// Function to handle error
}
@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
Logcat跟踪
08-29 11:57:01.292: E/AndroidRuntime(10924): FATAL EXCEPTION: main
08-29 11:57:01.292: E/AndroidRuntime(10924): Process: com.eps.fortalented, PID: 10924
08-29 11:57:01.292: E/AndroidRuntime(10924): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=32665, result=-1, data=Intent { (has extras) }} to activity {com.eps.fortalented/com.eps.fortalented.ui.LoginActivity}: java.lang.NullPointerException
08-29 11:57:01.292: E/AndroidRuntime(10924): at android.app.ActivityThread.deliverResults(ActivityThread.java:3700)
08-29 11:57:01.292: E/AndroidRuntime(10924): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3743)
08-29 11:57:01.292: E/AndroidRuntime(10924): at android.app.ActivityThread.access$1400(ActivityThread.java:172)
08-29 11:57:01.292: E/AndroidRuntime(10924): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
08-29 11:57:01.292: E/AndroidRuntime(10924): at android.os.Handler.dispatchMessage(Handler.java:102)
08-29 11:57:01.292: E/AndroidRuntime(10924): at android.os.Looper.loop(Looper.java:146)
08-29 11:57:01.292: E/AndroidRuntime(10924): at android.app.ActivityThread.main(ActivityThread.java:5653)
08-29 11:57:01.292: E/AndroidRuntime(10924): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 11:57:01.292: E/AndroidRuntime(10924): at java.lang.reflect.Method.invoke(Method.java:515)
08-29 11:57:01.292: E/AndroidRuntime(10924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
08-29 11:57:01.292: E/AndroidRuntime(10924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
08-29 11:57:01.292: E/AndroidRuntime(10924): at dalvik.system.NativeStart.main(Native Method)
08-29 11:57:01.292: E/AndroidRuntime(10924): Caused by: java.lang.NullPointerException
08-29 11:57:01.292: E/AndroidRuntime(10924): at com.eps.fortalented.ui.LoginActivity$4.onComplete(LoginActivity.java:435)
08-29 11:57:01.292: E/AndroidRuntime(10924): at com.facebook.android.Facebook.authorizeCallback(Facebook.java:399)
08-29 11:57:01.292: E/AndroidRuntime(10924): at com.eps.fortalented.ui.LoginActivity.onActivityResult(LoginActivity.java:465)
08-29 11:57:01.292: E/AndroidRuntime(10924): at android.app.Activity.dispatchActivityResult(Activity.java:5734)
08-29 11:57:01.292: E/AndroidRuntime(10924): at android.app.ActivityThread.deliverResults(ActivityThread.java:3696)