showFragment不是有效的方法

时间:2015-03-23 06:47:52

标签: java android facebook

我正在尝试使用本指南建立一个facebook登录:https://developers.facebook.com/docs/android/scrumptious/authenticate,我在showFragment(这甚至不是一件事)上得到错误,而findFragmentByID正在说我正在指定的片段无效(尝试片段类和片段布局不起作用)。 任何帮助? 代码:

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;

import static com.eren.valour.R.id.*;

public class MainScreen extends ActionBarActivity {
    private static final int FBSPLASH = 0;
    private static final int FBSELECTION = 1;
    private static final int FBFRAGMENT_COUNT = FBSELECTION + 1;
    private Fragment[] fragments = new Fragment[FBFRAGMENT_COUNT];
    ImageButton fblogin;
    Session session = Session.getActiveSession();
    private boolean isResumed = false;
    private UiLifecycleHelper uiHelper;
    private Session.StatusCallback callback =
            new Session.StatusCallback() {
                @Override
                public void call(Session session,
                                 SessionState state, Exception exception) {
                    onSessionStateChange(session, state, exception);
                }
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        setTitle("Valour");
        setContentView(R.layout.activity_main_screen);

        FragmentManager fm = getSupportFragmentManager();
        fragments[FBSPLASH] = fm.findFragmentById(R.id.fragment_facebook_login_dialogue);
        fragments[FBSELECTION] = fm.findFragmentById(R.id.FacebookLoginSelectFragment);

        FragmentTransaction transaction = fm.beginTransaction();
        for (int i = 0; i < fragments.length; i++) {
            transaction.hide(fragments[i]);
        }
        transaction.commit();
        getScreenRes();
    }

    @Override
    public void onResume() {
        super.onResume();
        isResumed = true;
    }

    @Override
    public void onPause() {
        super.onPause();
        isResumed = false;
    }

    public void getScreenRes() {
        DisplayMetrics display = this.getResources().getDisplayMetrics();
        int screenwidth = display.widthPixels;
        double buttonheight = screenwidth / 2.66666667;
        int screenheight = (int) Math.round(buttonheight);

//      ImageButton serviceList = (ImageButton) findViewById(R.id.addservice);
//      ViewGroup.LayoutParams servicelist = serviceList.getLayoutParams();
//      servicelist.width = screenwidth;
//      servicelist.height = screenheight;


        //Get screen dimensions and define button variables
        ImageButton redditLogin = (ImageButton) findViewById(R.id.redditLogin);
        ViewGroup.LayoutParams reddit = redditLogin.getLayoutParams();
        reddit.width = screenwidth;
        reddit.height = screenheight;

        ImageButton fbLogin = (ImageButton) findViewById(R.id.facebookLogin);
        ViewGroup.LayoutParams fb = fbLogin.getLayoutParams();
        fb.width = screenwidth;
        fb.height = screenheight;


        ImageButton instaLogin = (ImageButton) findViewById(R.id.instagramLogin);
        ViewGroup.LayoutParams insta = instaLogin.getLayoutParams();
        insta.width = screenwidth;
        insta.height = screenheight;

        ImageButton twitLogin = (ImageButton) findViewById(R.id.twitterLogin);
        ViewGroup.LayoutParams twit = twitLogin.getLayoutParams();
        twit.width = screenwidth;
        twit.height = screenheight;

//      set button size
        instaLogin.setLayoutParams(insta);
        fbLogin.setLayoutParams(fb);
        twitLogin.setLayoutParams(twit);
        redditLogin.setLayoutParams(reddit);
    }

    public void facebookLogin(View v) {

        if (session == null) {
            session = new Session(getApplicationContext());
        }
        Session.setActiveSession(session);

    }

    public void instagramLogin(View v) {
        serviceIncomplete();


    }

    public void redditLogin(View v) {
        serviceIncomplete();


    }

    public void twitterLogin(View v) {
        serviceIncomplete();


    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        // Only make changes if the activity is visible
        if (isResumed) {
            FragmentManager manager = getSupportFragmentManager();
            // Get the number of entries in the back stack
            int backStackSize = manager.getBackStackEntryCount();
            // Clear the back stack
            for (int i = 0; i < backStackSize; i++) {
                manager.popBackStack();
            }
            if (state.isOpened()) {
                // If the session state is open:
                // Show the authenticated fragment
                showFragment(FBSELECTION, false);
            } else if (state.isClosed()) {
                // If the session state is closed:
                // Show the login fragment
                showFragment(FBSPLASH, false);
            }
        }
    }

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        Session session = Session.getActiveSession();

        if (session != null && session.isOpened()) {
            // if the session is already open,
            // try to show the selection fragment
            showFragment(FBSELECTION, false);
        } else {
            // otherwise present the splash screen
            // and ask the person to login.
            showFragment(FBSPLASH, false);
        }
    }

1 个答案:

答案 0 :(得分:0)

  

showFragment不是有效的方法

Step 2: Wire up the authentication logic所示,在showFragment类中创建MainActivity方法,该方法正在扩展FragmentActivity

因此,showFragment方法不是来自facebook库,您需要在MainScreen Activity中创建它以在当前Activity中添加和删除片段。