简体   繁体   English

具有Facebook登录名的Android应用:该应用仅在授予权限并崩溃后才能运行

[英]Android app with facebook login: App only works after permissions granted and a crash

When the app starts for the first time and the user grants permission, the app crashes. 当应用程序首次启动且用户授予权限时,该应用程序将崩溃。 If the app is restarted the user is already logged in and the app works perfectly. 如果重新启动该应用程序,则该用户已经登录并且该应用程序可以正常运行。

To recreate the error I have to delete the app from the phone and disassociate the app from my Facebook account. 要重新创建错误,我必须从手机中删除该应用程序,然后将该应用程序与我的Facebook帐户解除关联。 ( if this helps ) (如果有帮助)

Also to clarify, I understand what a null pointer exception is, just don't quite understand why it is happening on only the first run. 同样要澄清的是,我了解什么是空指针异常,只是不太了解为什么它仅在第一次运行时才会发生。 Do I need to add some kind of statement for the first time run? 我是否需要在首次运行时添加某种声明?

            @Override
        protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    Parse.enableLocalDatastore(this);
    Parse.initialize(this);
    //parse test
    ParseObject testObject = new ParseObject("TestObject");
    testObject.put("foo", "bar");
    testObject.saveInBackground();

    callbackManager = CallbackManager.Factory.create();

    setContentView(R.layout.activity_main_page);
    info = (TextView) findViewById(R.id.info);
    loginButton = (LoginButton) findViewById(R.id.login_button);

    Bundle logout_req = getIntent().getExtras();
    if (logout_req != null) {
        String logout = logout_req.getString("user_logout");
        if (logout == "logout") {
            LoginManager.getInstance().logOut();
        }
    }

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);


    if (Profile.getCurrentProfile() != null) {
        Profile profile1 = Profile.getCurrentProfile();
        Intent myIntent = new Intent(getBaseContext(), Home.class);
        myIntent.putExtra("user_id", profile1.getId());
        myIntent.putExtra("user_name", profile1.getName());
        MainPage.this.startActivity(myIntent);
    }


            loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                Profile profile = Profile.getCurrentProfile();
                ParseUser.logInInBackground(profile.getId(), "0", new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user == null) {

                            //No such user, create a new user.
                            ParseUser newUser = new ParseUser();

                            Profile profile = Profile.getCurrentProfile();
                            newUser.setUsername(profile.getId());
                            newUser.setPassword("0");
                            newUser.signUpInBackground();

                            //Send required data to home page
                            Intent myIntent = new Intent(getBaseContext(), Home.class);
                            myIntent.putExtra("user_id", profile.getId());
                            myIntent.putExtra("user_name", profile.getName());
                            MainPage.this.startActivity(myIntent);
                            System.out.println("NEW USER CREATED! LOGGED IN AND SENT TO HOMEPAGE");


                        } else {

                            Profile profile = Profile.getCurrentProfile();
                            //Send required data to home page
                            Intent myIntent = new Intent(getBaseContext(), Home.class);
                            myIntent.putExtra("user_id", profile.getId());
                            myIntent.putExtra("user_name", profile.getName());
                            MainPage.this.startActivity(myIntent);
                            System.out.println("USER LOGGED IN! SENT TO HOMEPAGE");
                        }
                    }
                });
            }

            @Override
            public void onCancel() {
                info.setText("Login attempt canceled    .");
            }

            @Override
            public void onError(FacebookException e) {

                info.setText("Login attempt failed.");
            }
        });
    }

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

The error appears to happen just after loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() 该错误似乎仅在loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()

The error: 错误:

Process: com.george.coffeeconversation, PID: 16920
  java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {com.george.coffeeconversation/com.george.coffeeconversation.MainPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.facebook.Profile.getId()' on a null object reference
  at android.app.ActivityThread.deliverResults(ActivityThread.java:3680)
  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3723)
  at android.app.ActivityThread.access$1300(ActivityThread.java:156)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:211)
  at android.app.ActivityThread.main(ActivityThread.java:5373)
  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:1020)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.facebook.Profile.getId()' on a null object reference
  at com.george.coffeeconversation.MainPage.run(MainPage.java:108)
  at com.george.coffeeconversation.MainPage$1.onSuccess(MainPage.java:90)
  at com.george.coffeeconversation.MainPage$1.onSuccess(MainPage.java:87)
  at com.facebook.login.LoginManager.finishLogin(LoginManager.java:510)
  at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:193)
  at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:136)
  at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:82)
  at com.george.coffeeconversation.MainPage.onActivityResult(MainPage.java:144)
  at android.app.Activity.dispatchActivityResult(Activity.java:6192)
  at android.app.ActivityThread.deliverResults(ActivityThread.java:3676)
  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3723) 
  at android.app.ActivityThread.access$1300(ActivityThread.java:156) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:211) 
  at android.app.ActivityThread.main(ActivityThread.java:5373) 
  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:1020) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.facebook.Profile.getId()' on a null object reference 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String com.facebook.Profile.getId()'

Apparently the Profile object you're trying to access is null at the point in time you're referencing it. 显然,您要访问的Profile对象在引用该时间点时为null。

Since you mentioned it's occurring after returning from Facebook, it's probably happening in either done(ParseUser user, ParseException e) or onSuccess(LoginResult loginResult) . 既然您提到它是在从Facebook返回后发生的,所以它可能发生在done(ParseUser user, ParseException e)onSuccess(LoginResult loginResult)

Try logging whether Profile.getCurrentProfile() is null at those points and you'll be able to track it down. 尝试记录在那一点上Profile.getCurrentProfile()是否为null,您就可以对其进行跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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