onActivityResult调用了两次 [英] onActivityResult called twice

查看:372
本文介绍了onActivityResult调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Facebook按钮的简单活动,可以使用登录名(使用Facebook Android SDK:

I have a simple activity with a facebook button to use login (using Facebook Android SDK:

package com.example.adminn.facebooktest;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.content.Intent;
import android.view.View;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;

import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.CallbackManager;
import com.facebook.login.LoginManager;
import com.facebook.login.widget.LoginButton;
import com.facebook.login.LoginResult;
import com.facebook.FacebookCallback;

import java.util.Arrays;


public class MainActivity extends FragmentActivity {
    CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(this.getApplicationContext());
        setContentView(R.layout.activity_main);

        callbackManager = CallbackManager.Factory.create();


        //LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                //Toast toast = Toast.makeText(MainActivity.this, "OnSuccess:" + loginResult.getAccessToken().toString(), Toast.LENGTH_LONG);
                //toast.show();

            }

            @Override
            public void onCancel() {
                Toast toast = Toast.makeText(MainActivity.this, "Cancel!!", Toast.LENGTH_LONG);
                toast.show();
            }

            @Override
            public void onError(FacebookException e) {
                Toast toast = Toast.makeText(MainActivity.this, "Error!", Toast.LENGTH_LONG);
                toast.show();
            }
        });




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

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

    }

    public void facebookLogin(View v){

        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile","user_friends"));

    }
}

当我按下按钮时,上面的代码崩溃,并显示以下错误:

The code above crashes once i press the button, with the following error:

Process: com.example.adminn.facebooktest, PID: 8830
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {com.example.adminn.facebooktest/com.example.adminn.facebooktest.MainActivity}: java.lang.NullPointerException: Argument 'context' cannot be null
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3626)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3669)
            at android.app.ActivityThread.access$1300(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            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:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.NullPointerException: Argument 'context' cannot be null
            at com.facebook.internal.Validate.notNull(Validate.java:67)
            at com.facebook.appevents.AppEventsLogger.<init>(AppEventsLogger.java:652)
            at com.facebook.appevents.AppEventsLogger.newLogger(AppEventsLogger.java:417)
            at com.facebook.login.LoginLogger.<init>(LoginLogger.java:67)
            at com.facebook.login.LoginManager.getLogger(LoginManager.java:397)
            at com.facebook.login.LoginManager.logCompleteLogin(LoginManager.java:415)
            at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:190)
            at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:140)
            at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:82)
            at com.example.adminn.facebooktest.MainActivity.onActivityResult(MainActivity.java:88)
            at android.app.Activity.dispatchActivityResult(Activity.java:6161)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3622)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3669)
            at android.app.ActivityThread.access$1300(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            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:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

我已经使用断点在SDK的源代码中进行了一些挖掘,我意识到onActivityResult被调用了两次(当我无缘无故地按下按钮和另一个按钮时).第一次顺利进行并检索令牌和会话信息.完成后,LoginManager.java中的上下文字段将设置为null.由于callbackManager.onActivityResult有时会调用logCompleteLogin(后者又根据空上下文创建LoginLogger),因此应用程序崩溃.

I have done some digging through the SDK's source code using breakpoints and i realized that the onActivityResult is being called twice (when i press the button and another one for no reason). The first time it goes smooth and retrieves the token and session information. When it finishes, the context field inside LoginManager.java is set to null. Since callbackManager.onActivityResult calls, at some point, logCompleteLogin, which in turn creates a LoginLogger based on the nulled context, the application crashes.

为什么onActivityResult被调用两次?据我所知,除MainActivity之外,只有另一个活动,即FacebookActivity.

Why is onActivityResult being called twice? As far as i know, there is only another activity besides MainActivity, which is FacebookActivity.

推荐答案

这两个问题都已解决且相关.

Both problems are solved and they are related.

发生了什么,我无法理解,是因为facebook按钮具有触发触发登录过程的潜在事件.我在MainActivity上设置了另一个事件,以便在点击时采取行动.因此,在内部发生的事情是,两次创建了facebookactivity,这导致两次调用onActivityResult.第一次调用onActivityResult会使LoginManager的内部状态保留为空的上下文字段(以前是MainActivity).第二次调用假定上下文不为null,如果上下文为null,则会使应用程序崩溃(内部存在Validate验证).

What happens and i failed to understand was that the facebook button has an underlying event that triggers the login process. I set another event on MainActivity to act upon click. So, internally, what was happening was that, the facebookactivity was being created twice which lead to two calls to onActivityResult. The first call to onActivityResult leaves the internal state of LoginManager with a nulled context field (which was previously the MainActivity). A second call assumes that the context is not null, crashing the application if it is (there is a Validate verification internally).

回退到SDK的早期版本是可行的,因为在调用logInWithReadPermissions时LogginManager.context没有被无效,从而使第二个调用有效".

Falling back to a previous version of the SDK worked because LogginManager.context was not being nullified when calling logInWithReadPermissions, rendering the second call "valid".

这篇关于onActivityResult调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆