在将facebook登录与我的Android应用程序集成时,我有空指针异常。我按照this教程。我也附上了书面代码。 FacebookLogin.java
public class FacebookLogin extends FragmentActivity{
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
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();
}
@Override
public void onResume() {
super.onResume();
isResumed = true;
uiHelper.onResume();
isResumed = true;
}
@Override
public void onPause() {
super.onPause();
isResumed = false;
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);
} };
}
SplashFragment.java
public class SplashFragment extends Fragment {
private Button skipLoginButton;
private SkipLoginCallback skipLoginCallback;
public interface SkipLoginCallback {
void onSkipLoginPressed();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.splash, container, false);
skipLoginButton = (Button) view.findViewById(R.id.login_button);
skipLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (skipLoginCallback != null) {
skipLoginCallback.onSkipLoginPressed();
} } });
return view;
}
public void setSkipLoginCallback(SkipLoginCallback callback) {
skipLoginCallback = callback;
}}
main.xml中
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- <fragment android:name="com.GenioCode.replay.SelectionFragment"
android:id="@+id/selectionFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->
<fragment android:name="com.GenioCode.replay.SplashFragment"
android:id="@+id/splashFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
logcat的
12-22 01:12:05.432: E/AndroidRuntime(1555): FATAL EXCEPTION: main
12-22 01:12:05.432: E/AndroidRuntime(1555): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.GenioCode.replay/com.GenioCode.replay.FacebookLogin}: java.lang.NullPointerException
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.os.Looper.loop(Looper.java:137)
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-22 01:12:05.432: E/AndroidRuntime(1555): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 01:12:05.432: E/AndroidRuntime(1555): at java.lang.reflect.Method.invoke(Method.java:525)
12-22 01:12:05.432: E/AndroidRuntime(1555): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-22 01:12:05.432: E/AndroidRuntime(1555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-22 01:12:05.432: E/AndroidRuntime(1555): at dalvik.system.NativeStart.main(Native Method)
12-22 01:12:05.432: E/AndroidRuntime(1555): Caused by: java.lang.NullPointerException
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:656)
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.app.Activity.performStart(Activity.java:5143)
12-22 01:12:05.432: E/AndroidRuntime(1555): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
12-22 01:12:05.432: E/AndroidRuntime(1555): ... 11 more
请帮我解决这个例外。
答案 0 :(得分:0)
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
片段数组的长度为2;
所以当你打电话时
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();
}
with fragmentIndex = 1,为空,因为你已注释了行
// fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
所以它试图显示一个空片段(因为即使你只有一个片段,循环仍会运行两次)
可能会为您提供null Pointer Exception