简体   繁体   English

Facebook SDK仅适用于活动吗?

[英]Does the Facebook SDK only work with Activities?

I've been trying to implement the Facebook SDK into my application in order to let users post messages on our fanwall through the app. 我一直在尝试将Facebook SDK应用到我的应用程序中,以便让用户通过应用程序在我们的粉丝墙上发布消息。 However, I've been unsuccessful at even logging the user in through the SDK. 但是,即使通过SDK登录用户,我也没有成功。

In the SDK examples, a simple sample has been given which uses an Activity to try and authorize the user using Single Sign-On. 在SDK示例中,给出了一个简单的示例,该示例使用Activity尝试使用Single Sign-On来授权用户。 I've tried this example myself, and it works. 我自己试过这个例子,但它确实有效。 I'm able to log in, I had to authorize the app to use my Facebook data and I could see the requests being made and received in LogCat. 我能够登录,我必须授权应用程序使用我的Facebook数据,我可以看到在LogCat中发出和接收的请求。

Now, I've tried adding the same code to my own app. 现在,我尝试将相同的代码添加到我自己的应用程序中。 This app is Fragment based using the Compatibility package. 此应用程序是基于Fragment使用兼容性包。 There is one central FragmentActivity and the rest of my classes are simple Fragments . 有一个中心FragmentActivity ,其余的类是简单的Fragments When adding the sample code to one of these Fragments , the Facebook app starts up for half a second when trying to authorize, but afterwards closes and nothing has happened. 将示例代码添加到其中一个Fragments ,Facebook应用程序在尝试授权时会启动半秒钟,但之后关闭并且没有任何事情发生。 I'm back in my regular Fragment again. 我又回到了常规碎片中。

When checking the LogCat after this, nothing suggests that the Facebook app even opened or made any requests other than the fact it shows some printing checks I added and the fact it says it's starting the Facebook intent : 在此之后检查LogCat时,没有任何迹象表明Facebook应用程序甚至打开或提出任何请求,除了它显示我添加的一些打印检查这一事实,它说它启动了Facebook的意图:

01-12 13:19:40.874: I/System.out(6087): Calling authorize
01-12 13:19:40.874: I/ActivityManager(1380): Starting activity: Intent { cmp=com.facebook.katana/.ProxyAuth (has extras) } from pid 6087
01-12 13:19:40.874: I/System.out(6087): Called authorize

Other than that, nothing gets returned. 除此之外,没有任何东西返回。 No Facebook-checks, no statements saying my keys are wrong or anything, just nothing. 没有Facebook检查,没有声明说我的钥匙是错的或什么都没有。 The Facebook intent was called, but closed almost immediately and nothing else shows that it was even open. Facebook的意图被召唤,但几乎立即关闭,没有任何其他表明它甚至是开放的。

This has been boggling my mind for a few hours now, and I'm starting to think the regular , sample-provided approach just doesn't work in Fragments due to the way Fragments work. 这已经令人难以置信我的脑海里了几个小时,现在,我开始觉得规则,样本提供的方法就是不工作Fragments ,由于道路Fragments的工作。

The code I've been using is posted below. 我一直在使用的代码发布在下面。 The Fragment gets fired by a button which calls a FragmentTransaction . Fragment被一个调用FragmentTransaction的按钮触发。 Am I doing something fundamentally wrong here, or does the Facebook SDK really just not work with Fragments ? 我在这里做了一些根本错误的事情,或者Facebook SDK真的不能与Fragments吗? I've tried searching for this problem but I haven't been able to find anyone else with the same sort of situation. 我已经尝试过寻找这个问题,但我找不到其他同样情况的人。


public class FanWallFacebook extends Fragment {

    Facebook facebook = new Facebook("294678133912628");

    public FanWallFacebook() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.filler, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();

        System.out.println("Calling authorize");
        facebook.authorize(getActivity(), new DialogListener() {
            @Override
            public void onComplete(Bundle values) {
                System.out.println("Completed");
            }

            @Override
            public void onFacebookError(FacebookError error) {
                System.out.println("Facebook error: "+error.getMessage());
            }


            @Override
            public void onError(DialogError e) {
                System.out.println("General error: "+e.getMessage());
            }

            @Override
            public void onCancel() {
                System.out.println("Cancelled");
            }
        });

        System.out.println("Called authorize");

    }

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

        facebook.authorizeCallback(requestCode, resultCode, data);
        System.out.println("Authorize callback'd");
    }

}

Edit 编辑

Just tried using a FragmentActivity , and lo and behold, that does work. 刚尝试使用FragmentActivity ,并且看哪,这确实有效。 It successfully logs in. Seems like the SDK really only works with classes that explicitly extend ...Activity . 它成功登录。似乎SDK真的只适用于显式扩展的类...Activity Could anyone give me an idea why that might be the case? 任何人都可以告诉我为什么会这样吗? I always thought Fragments somewhere down the line extended Activity as well. 我一直认为Fragments某个地方延伸了Activity

well... 好...

the new Facebook SDK has an Activity that implement FragmentActivity so problem solved. 新的Facebook SDK有一个实现FragmentActivity的Activity,所以问题解决了。

I ended up using a FragmentActivity for my Facebook interaction. 我最终使用FragmentActivity进行Facebook交互。 Not the ideal solution, but it works. 不是理想的解决方案,但它确实有效。

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

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