2

I'm trying to use the Facebook 3.0 SDK's login by following this guide from the facebook developer's: https://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/authenticate/

my problem is that when the user clicks the login button, my activity closes and Facebook's process dies.

this is the logcat from Android Studio:

08-20 12:17:40.124      353-353/system_process I/ActivityManager: START u0 {act=SSO_WITH_FALLBACK cmp=com.my.app/com.facebook.LoginActivity (has extras)} from pid 30362
08-20 12:17:40.434      353-370/system_process I/ActivityManager: Displayed com.my.app/com.facebook.LoginActivity: +268ms
08-20 12:17:44.094      353-546/system_process I/ActivityManager: Start proc android.process.acore for content provider com.android.providers.contacts/.ContactsProvider2: pid=30500 uid=10014 gids={50014, 3003, 1015, 1028}
08-20 12:17:44.134  30500-30500/android.process.acore E/Trace: error opening trace file: No such file or directory (2)
08-20 12:17:45.494    353-12974/system_process I/ActivityManager: Process com.facebook.katana:dash (pid 30233) has died.

Any suggestion on my problem?

EDIT: Here's the code of the main activity

public class MainActivity extends FragmentActivity {
    public static final boolean D = SystemConstants.ACTIVE_DEBUG;
    public static final String TAG = "MainActivity";

    private static final int SPLASH = 0;
    private static final int SELECTION = 1;
    private static final int FRAGMENT_COUNT = SELECTION +1;

    private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
    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);

        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FragmentManager fm = getSupportFragmentManager();
        fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
        fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);

        FragmentTransaction transaction = fm.beginTransaction();
        for (Fragment fragment : fragments) {
            transaction.hide(fragment);
        }
        transaction.commit();
    }

    /**
     * Configure files destinations.
     */
    private void configureEnvironment() {
        File sd = Environment.getExternalStorageDirectory();
        if (sd.canWrite()) {
            File destination = new File(sd, SettingConstants.BASE_DIR);
            if (!destination.mkdir() && !destination.isDirectory()) {
                Log.e(TAG, "Unable to create Base Directory.");
                Tracking.sendException(new IllegalStateException("Unable to create Base Directory."));
            }

            File audio = new File(sd, SettingConstants.AUDIO_DIR);
            if (!audio.mkdir() && !audio.isDirectory()) {
                Log.e(TAG, "Unable to create Audio Directory.");
                Tracking.sendException(new IllegalStateException("Unable to create Audio Directory."));
            }

            File avatar = new File(sd, SettingConstants.AVATAR_DIR);
            if (!avatar.mkdir() && !avatar.isDirectory()) {
                Log.e(TAG, "Unable to create Avatar Directory.");
                Tracking.sendException(new IllegalStateException("Unable to create Avatar Directory."));
            }

            File image = new File(sd, SettingConstants.IMAGE_DIR);
            if (!image.mkdir() && !image.isDirectory()) {
                Log.e(TAG, "Unable to create Image Directory.");
                Tracking.sendException(new IllegalStateException("Unable to create Image Directory."));
            }

            File video = new File(sd, SettingConstants.VIDEO_DIR);
            if (!video.mkdir() && !video.isDirectory()) {
                Log.e(TAG, "Unable to create Video Directory.");
                Tracking.sendException(new IllegalStateException("Unable to create Video Directory."));
            }

        }
    }

    /**
     * Shows a fragment
     * @param fragmentIndex
     * @param addToBackStack
     */
    private void showFragment(int fragmentIndex, boolean addToBackStack) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i = 0; i < fragments.length; i++) {
            if (i == fragmentIndex) {
                transaction.show(fragments[i]);
            } else {
                transaction.hide(fragments[i]);
            }
        }
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }

    /**
     * called due to session state changes. The method shows the relevant fragment based on the person's authenticated state.
     * @param session Facebook Session
     * @param state Facebook login state
     * @param exception Eventual exception
     */
    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(SELECTION, false);
            } else if (state.isClosed()) {
                // If the session state is closed:
                // Show the login fragment
                showFragment(SPLASH, false);
            }
        }
    }

    /**
     * case where fragments are newly instantiated and the authenticated versus nonauthenticated UI needs to be properly set.
     */
    @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(SELECTION, false);
        } else {
            // otherwise present the splash screen
            // and ask the person to login.
            showFragment(SPLASH, false);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Tracking.startActivityTracking(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        Tracking.stopActivityTracking(this);
    }

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

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

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

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }
}

EDIT 2: Tried on the emulator gives the same error, given that the facebook APK is not installed it shows a webview, asks for login and then closes the activity. I've added method tracing logging, the last call inside MainActivity is onDestroy...

4

1 回答 1

1

刚刚找到了解决这个问题的方法。

确保你不使用

android:noHistory="true"

在清单中,相对于 MainActivity(在给定的示例中)。

于 2013-08-20T15:13:36.190 回答