2013-07-22 57 views
-1

我已經跟着Facebook的Android教程,據我可以告訴線路登錄認證。然而,我有問題。當我擁有elogge din時,我的「你現在登錄」片段永遠不會顯示。我相信這是與showFragment方法。請在下面找到它:Android的Facebook教程 - 登錄後不顯示片段

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(); 
} 

當放入一個sys.out,fragmentIndex也是0,因此我的else片段永遠不會顯示。有什麼明顯的我失蹤?任何幫助將不勝感激。

請找班上的其他同學和XML只是櫃面:

package com.example.social; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.view.Menu; 
import android.view.MenuItem; 

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



public class DashBoard extends FragmentActivity { 

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

private Fragment[] fragments = new Fragment[FRAGMENT_COUNT]; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    uiHelper = new UiLifecycleHelper(this, callback); 
    uiHelper.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_dash_board); 

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

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

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(); 
} 

public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public void facebookIntent(MenuItem item) { 
    startActivity(new Intent(this, Facebook.class)); 

} 

public void twitterIntent(MenuItem item) { 
    startActivity(new Intent(this, Twitter.class)); 

} 

public void dashboardIntent(MenuItem item) { 
    startActivity(new Intent(this, DashBoard.class)); 

} 

public void redditIntent(MenuItem item) { 
    startActivity(new Intent(this, Reddit.class)); 

} 

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

} 

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

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); 
     } 
    } 
} 

@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); 
    } 
} 

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 
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); 
} 

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    tools:context=".DashBoard" > 

    <fragment 
     android:id="@+id/selectionFragment" 
     android:name="com.example.social.SelectionFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <fragment 
     android:id="@+id/splashFragment" 
     android:name="com.example.social.SplashFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 
+0

任何人有任何想法?現在已經卡住了一個星期左右,謝謝你們, – Programatt

+0

另一個凹凸 - 把我的頭髮拉出來,然後重新開始,只是遇到了同樣的問題 – Programatt

+0

這似乎是一個不規則的問題,因爲我找不到回答其他地方,這是沒有得到答案。這幾乎肯定是我做錯了,所以我真的可以上任何反饋。 – Programatt

回答