我創建了一個帶有facebook按鈕的片段。它工作了一段時間,現在當我試圖使用導航應用程序抽屜來打開片段時,它正在崩潰應用程序。Facebook按鈕片段不打開並終止應用程序
下面是堆棧跟蹤:
E/AndroidRuntime:致命異常:主 工藝:com.myclass.myapp,PID:3526 java.lang中。 NullPointerException:嘗試調用虛擬方法'void com.facebook.AccessTokenTracker。上在com.myclass.myapp.FacebookLogin.onCreate空對象引用 startTracking()」 (FacebookLogin的.java:82) 在android.support.v4.app.Fragment.performCreate(Fragment.java: 2177) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1244) at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1080) at android.support.v4。 app.FragmentTransition.calculateFragments(FragmentTransition.java:971) at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95) at android.support.v4.app.Fra gmentManagerImpl.executeOpsTogether(FragmentManager.java:2143) at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008) at android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:710) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java :95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller .run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 終止應用程序。
代碼片段類:
public class FacebookLogin extends Fragment {
//initialize WebView
private TextView mTextDetails;
private CallbackManager mCallbackManager;
private AccessTokenTracker mTokenTracker;
private ProfileTracker mProfileTracker;
private FacebookCallback<LoginResult> mCallback=new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
displayWelcomeMessage(profile);
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallbackManager=CallbackManager.Factory.create();
AccessTokenTracker tracker=new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
}
};
ProfileTracker profileTracker=new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
displayWelcomeMessage(currentProfile);
}
};
mTokenTracker.startTracking();
mProfileTracker.startTracking();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.facebook_login, container, false);
}
private void displayWelcomeMessage(Profile profile){
if (profile != null){
mTextDetails.setText(profile.getName());
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
loginButton.setFragment(this);
loginButton.registerCallback(mCallbackManager,mCallback);
mTextDetails = (TextView)view.findViewById(R.id.text_details);
}
@Override
public void onResume() {
super.onResume();
Profile profile=Profile.getCurrentProfile();
displayWelcomeMessage(profile);
}
@Override
public void onStop(){
super.onStop();
mTokenTracker.stopTracking();
mProfileTracker.stopTracking();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode,resultCode, data);
}
}
請粘貼代碼也...我們想知道喲你做了。 –
@DharmbirSingh。更新代碼,,我用應用程序類來生成hashkey ... –
檢查下面的答案... – rafsanahmad007