0
所以,我正在練習這個代碼,我從來不知道出了什麼問題。我跟着它在本教程中的確切說法https://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/authenticate/Facebook的SDK 3.0示例
這裏是我的代碼:應用程序意外停止。請幫助
public class MainActivity extends FragmentActivity {
private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int SETTINGS = 2;
private static final int FRAGMENT_COUNT = SETTINGS +1;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private MenuItem settings;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
setContentView(R.layout.main);
FragmentManager fm = getSupportFragmentManager();
fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
fragments[SETTINGS] = fm.findFragmentById(R.id.userSettingsFragment);
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();
}
private boolean isResumed = false;
@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);
}
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 boolean onPrepareOptionsMenu(Menu menu) {
// only add the menu when the selection fragment is showing
if (fragments[SELECTION].isVisible()) {
if (menu.size() == 0) {
settings = menu.add(R.string.settings);
}
return true;
} else {
menu.clear();
settings = null;
}
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.equals(settings)) {
showFragment(SETTINGS, true);
return true;
}
return false;
}
}
05-21 05:46:22.031: D/dalvikvm(527): GC_EXTERNAL_ALLOC freed 89K, 52% free 2603K/5379K, external 1808K/2137K, paused 89ms
05-21 05:46:22.221: D/AndroidRuntime(527): Shutting down VM
05-21 05:46:22.221: W/dalvikvm(527): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-21 05:46:22.241: E/AndroidRuntime(527): FATAL EXCEPTION: main
05-21 05:46:22.241: E/AndroidRuntime(527): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firstandroidapp/com.firstandroidapp.MainActivity}: java.lang.NullPointerException
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.os.Looper.loop(Looper.java:123)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-21 05:46:22.241: E/AndroidRuntime(527): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 05:46:22.241: E/AndroidRuntime(527): at java.lang.reflect.Method.invoke(Method.java:507)
05-21 05:46:22.241: E/AndroidRuntime(527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-21 05:46:22.241: E/AndroidRuntime(527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-21 05:46:22.241: E/AndroidRuntime(527): at dalvik.system.NativeStart.main(Native Method)
05-21 05:46:22.241: E/AndroidRuntime(527): Caused by: java.lang.NullPointerException
05-21 05:46:22.241: E/AndroidRuntime(527): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:609)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.Activity.performStart(Activity.java:3791)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1620)
05-21 05:46:22.241: E/AndroidRuntime(527): ... 11 more
05-21 05:47:01.391: I/Process(527): Sending signal. PID: 527 SIG: 9
哪裏是你的logcat輸出 –
我編輯的帖子,你可以看到它右下方我的代碼 – user1954048