Android的 - 现有片段冲突与Facebook SDK? [英] Android - Existing Fragment Conflicting with Facebook SDK?

查看:107
本文介绍了Android的 - 现有片段冲突与Facebook SDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序和上午来的地步,我想Facebook登录集成到主界面。我下面这些方向是相同的:

I'm developing an Android app and am to the point where I would like to integrate Facebook login onto the main page. I am following these directions for the same:

https://developers.facebook.com/docs/android /登录,与Facebook的/#步骤1

在演练中列出改变MainActivity类来扩展FragmentActivity的一部分。我MainActivity类,但是,已经延长,因为我实现了对主视图内容滑块它。 MainActivity的原始的onCreate code是:

Part of the walkthrough lists changing the MainActivity class to extend FragmentActivity. My MainActivity class, however, already extended it because of a content slider I implemented on the main view. The original onCreate code of MainActivity was:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
             invalidateOptionsMenu();
        }
    });
}

这是伟大的工作。继Facebook登录演练中,我加入以下的指示了新的MainFragment类逐字地:

And this was working great. Following the Facebook login walkthrough, I added a new 'MainFragment' class following the directions as literally as possible:

public class MainFragment extends Fragment {

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

    return view;
    }
}

,还增加了一个com.facebook.widget.LoginButton我MainActivity布局(这似乎显示效果细腻),并试图通过添加以下到MainActivity类(上面显示的OnCreate部分,将其结合在一起,但不重复这一切在这里再次为简便起见):

, also added a com.facebook.widget.LoginButton to my MainActivity layout (this seems to display fine), and tried to tie it all together by adding the following to the OnCreate section of the MainActivity class (displayed above, but not repeating it all here again for brevity):

    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        mainFragment = new MainFragment();
        getSupportFragmentManager()
                .beginTransaction()
                .add(android.R.id.content, mainFragment)
                .commit();
    } else {
        // Or set the fragment from restored state info
        mainFragment = (MainFragment) getSupportFragmentManager()
                .findFragmentById(android.R.id.content);
    }

这个问题我基本上已经是如何让我的两个现有的滑块和Facebook登录玩起来好听?如果我添加需要MainActivity中的OnCreate Facebook的code,那么我的滑块消失。如果我不加code,那么Facebook登录显然是行不通的。我读过尽可能的片段想我可能会做什么不对劲的地方,但没有多少运气...

The question I fundamentally have is how to make both my existing slider and Facebook login play together nicely? If I add the Facebook code needed to OnCreate of MainActivity, then my slider goes away. If I don't add the code, then Facebook login obviously doesn't work. I've read as much as possible on Fragments thinking I might be doing something wrong there, but haven't had much luck...

在此先感谢您的帮助,您可以提供!

Thanks in advance for any help you can provide!

推荐答案

我能够辨别问题是在CreateView的方法MainFragment。出于某种原因,如果我删除此重写,我没有在我的内容sliderr被删除的问题。不知道为什么,而是把下面几行:LoginButton authButton =(LoginButton)findViewById(R.id.authButton); authButton.setFragment(本);在MainFragment的onViewCreate方法,我只是将它们移动到MainActivity的onCreate方法。似乎也为我工作,但不能完全肯定原来的问题是什么诚实。

I was able to discern the problem was in the MainFragment on CreateView method. For some reason, if I removed this override, I didn't have a problem where my content sliderr gets removed. No idea why, but instead of adding the following lines: LoginButton authButton = (LoginButton) findViewById(R.id.authButton); authButton.setFragment(this); in the onViewCreate method of MainFragment, I simply moved them to the onCreate method in MainActivity. Seems to have worked for me, although not entirely sure what the original problem was honestly.

这篇关于Android的 - 现有片段冲突与Facebook SDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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