我正在尝试使用以下指南建立一个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);
        }
    }

最佳答案

您的第二次findFragmentById调用未找到任何内容,因为您的参数是R.id。

您需要提供一个有效的ID才能找到您的片段。

09-29 22:53