1

我正在尝试在我的应用程序中实现 Facebook for Android。我正在使用 Android 2.0 的设备上运行我的应用程序。

我按照https://developers.facebook.com/docs/tutorials/androidsdk/3.0/scrumptious/authenticate/的说明创建了一个登录屏幕,但我不断遇到错误。

我看到一个登录按钮,我单击它。这会将我带到用户名和密码输入屏幕,在该屏幕上输入我的信息并单击登录。但是,一旦我单击登录,我的应用程序就会崩溃,并且出现以下错误。

ERROR/AndroidRuntime(4607): java.lang.NullPointerException
    at android.webkit.SslErrorHandler.handleMessage(SslErrorHandler.java:62)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:622)
    at java.lang.Thread.run(Thread.java:1096)

紧随其后的是:

ERROR/ActivityThread(4607): Activity com.facebook.LoginActivity has leaked IntentReceiver android.webkit.BrowserGateReceiver@47b94148 that was originally registered here. Are you missing a call to unregisterReceiver()?
    android.app.IntentReceiverLeaked: Activity com.facebook.LoginActivity has leaked IntentReceiver android.webkit.BrowserGateReceiver@47b94148 that was originally registered here. Are you missing a call to unregisterReceiver()?
    at android.app.ActivityThread$PackageInfo$ReceiverDispatcher.<init>(ActivityThread.java:797)
    at android.app.ActivityThread$PackageInfo.getReceiverDispatcher(ActivityThread.java:608)
    at android.app.ApplicationContext.registerReceiverInternal(ApplicationContext.java:813)
    at android.app.ApplicationContext.registerReceiver(ApplicationContext.java:800)
    at android.app.ApplicationContext.registerReceiver(ApplicationContext.java:794)
    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:308)
    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:308)
    at android.webkit.LoadListener.registerReceiver(LoadListener.java:1855)
    at android.webkit.LoadListener.<init>(LoadListener.java:191)
    at android.webkit.LoadListener.getLoadListener(LoadListener.java:163)
    at android.webkit.BrowserFrame.startLoadingResource(BrowserFrame.java:554)
    at android.webkit.LoadListener.nativeAddData(Native Method)
    at android.webkit.LoadListener.commitLoad(LoadListener.java:1256)
    at android.webkit.LoadListener.handleMessage(LoadListener.java:257)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:622)
    at java.lang.Thread.run(Thread.java:1096)

我已经搜索了两天没有成功找到这个问题的答案,但我找不到任何东西。我认为问题可能是我手机的Android版本太旧了。如果需要,我可以发布我的代码,但它与上面链接中的代码完全相同。任何帮助表示赞赏。谢谢。

这是我的活动课:

public class FacebookScreen 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];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);

    setContentView(R.layout.facebook_login_screen);

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

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

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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.example.SelectionFragment"
          android:id="@+id/selectionFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
<fragment android:name="com.example.SplashFragment"
          android:id="@+id/splashFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
</LinearLayout>
4

2 回答 2

2

2.2 之前的 Android 版本在 webkit 代码中存在导致这些空指针异常的错误。请参阅http://code.google.com/p/android/issues/detail?id=7900

Facebook SDK 3.0 在清单中的最低 Android 版本为 2.2 - 可能是因为这个错误?

于 2013-06-13T19:09:43.503 回答
0

您需要发布代码示例。它抛出一个 NULLPointerException,这可能是 Intent 泄漏的原因。如果您分享一个片段,我们至少可以知道哪里出了问题

于 2013-05-21T00:50:08.110 回答