简体   繁体   中英

Android Studio: Facebook Log in

I am trying to make a really basic Facebook login program for Android studio. I have followed all of the steps, exactly as described in at least 4 tutorials, but the result remains the same. When I open the app it crashes. So here is my code:

private FacebookCallback<LoginResult> mCallBack = new  FacebookCallback<LoginResult>(){
    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        Profile profile = Profile.getCurrentProfile();
        if(profile!=null){

            textlogin.setText("Hello "+profile.getFirstName());
        }
    }

    @Override
    public void onCancel() {
        // App code
    }

    @Override
    public void onError(FacebookException exception) {
        // App code
    }

};




  @Override
    public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        callbackManager=CallbackManager.Factory.create();

    }
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    LoginButton loginButton = (LoginButton)view.findViewById(R.id.login_button);
    textlogin = (TextView)view.findViewById(R.id.textView);
    loginButton.setReadPermissions("user_friends");
    loginButton.setFragment(this);
  loginButton.registerCallback(callbackManager, mCallBack);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    callbackManager.onActivityResult(requestCode,resultCode,data);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_main, container, false);

}

and here is the error log (it really is that short):

java.lang.NullPointerException
        at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:911)
        at com.facebook.internal.Utility.queryAppSettings(Utility.java:802)
        at com.facebook.login.widget.LoginButton$1.run(LoginButton.java:509)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)

Found error. Misplaced the <Metadata> . All working now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM