我通過stackoverflow搜索了很多,但我找不到解決我的問題的答案。因此我張貼這個。facebook登錄空指針異常android
我有一個示例Android應用程序,我試圖使用Facebook登錄。下面是我遵循的步驟:
環境:Android的工作室 的Android SDK版本:22 Facebook的SDK:4.0
遵循的步驟,以在模擬器上安裝FB APK,所產生的開發哈希鍵和更新它在應用程序 - >設置中。 我的Android清單包含這些代碼
<uses-permission android:name="android.permission.INTERNET"/>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar" android:label="@string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
在我activity_main.xml中佈局文件,我有這樣的一段代碼
<com.facebook.login.widget.LoginButton
android:id="@+id/fblogin_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
最後,在我的主要活動我有這個
public class MainActivity extends Activity {
LoginButton loginButton;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.fblogin_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(),"Fb Login Success",Toast.LENGTH_LONG);
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(),"Fb on cancel",Toast.LENGTH_LONG);
}
@Override
public void onError(FacebookException e) {
Toast.makeText(getApplicationContext(),"Fb Login Error",Toast.LENGTH_LONG);
}
});
setContentView(R.layout.activity_main);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Log.d("Inside Fb login","on Activiry result");
callbackManager.onActivityResult(requestCode, resultCode, data);
}}
我收到一個空指針異常,如下所示
java.lang.RuntimeException: Unable to start activity componentInfo{PROJECT.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.login.widget.LoginButton.registerCallback(com.facebook.CallbackManager, com.facebook.FacebookCallback)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我在這裏錯過了什麼?
放置FacebookSdk.sdkInitialize(getApplicationContext());在 之前setContentView(R.layout.activity_main);訣竅。感謝您的幫助@Daniel – user2752910 2015-04-10 06:02:43