0

我询问 facebook android sdk 身份验证错误。

当我运行程序时,出现错误“已意外停止。请重试。”

调试错误是关于 NullPointerException 问题。(facebook-android-sdk/facebook/src/com/facebook/internal/Validate.java)

我怎么解决这个问题?谢谢。

package com.example.fbloginau;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;


public class MainFragment extends FragmentActivity 
{

private Fragment Fragment;

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

    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        Fragment = new Fragment();
        getSupportFragmentManager()
        .beginTransaction()
        .add(android.R.id.content, Fragment)
        .commit();
    } 
    else 
    {
        // Or set the fragment from restored state info
        Fragment = (Fragment) getSupportFragmentManager()
        .findFragmentById(android.R.id.content);
    }
}
 }

块引用

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

<com.facebook.widget.LoginButton
    android:id="@+id/authButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    />

块引用

enter code here
4

2 回答 2

1

您可以使用 social auth sdk 进行 android facebook 集成,您可以在此处找到代码:

http://code.google.com/p/socialauth-android/downloads/list

http://www.codeproject.com/Tips/457153/How-to-integrate-Facebook-Twitter-Linkedin-in-Andr

于 2013-01-31T10:09:05.097 回答
0

您可以尝试通过一些网络服务获取访问令牌并进行初始化。您可以在活动中授权您的会话并通过活页夹传递它。脸书脸书

这里的例子。

public class ClientService extends Service {

    //Facebook object to operate from service
    private Facebook facebook;

...

    private final ClientBinder clientBinter = new ClientBinder();

    /** Binder to service */
    public class ClientBinder extends Binder {

        public ClientService getService() {
            return ClientService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return clientBinter;
    }

    public void setFacebook(Facebook f) {
        ClientService.this.facebook = f;
    }

    public boolean loggedFacebook() {
        if (facebook == null)
            return false;
        return facebook.isSessionValid();
    }

}




public class DemoActivity extends Activity{

        private ClientService clientService;
        private ServiceConnection clientConnection = new ServiceConnection() {

            public void onServiceDisconnected(ComponentName name) {
                mBound = false;
            }

            public void onServiceConnected(ComponentName name, IBinder service) {
                ClientService.ClientBinder binder = (ClientService.ClientBinder) service;
                clientService = binder.getService();
                mBound = true;
                if (facebook != null)
                    if (facebook.isSessionValid())
                        clientService.setFacebook(facebook);
            }
        };

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, ClientService.class);
        bindService(intent, clientConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mBound) {
            unbindService(clientConnection);
            mBound = false;
        }

    }




      private void logINFacebook() {
                if (facebook.isSessionValid())
                    return;

                facebook.authorize(this,
                        getResources().getStringArray(R.array.facebookPermissions),
                        new DialogListener() {
                            public void onFacebookError(FacebookError e) {...}

                            public void onError(DialogError e) {...}

                            public void onComplete(Bundle values) {
                                if (mBound) {
                                    clientService.setFacebook(facebook);
                                }

                                //Save to shared preferences(optional)
                                SharedPreferences.Editor editor = prefs.edit();
                                editor.putString(FACEBOOK_TOKEN_PREFNAME,
                                        facebook.getAccessToken());
                                editor.putLong(FACEBOOK_EXPIRES_PREFNAME,
                                        facebook.getAccessExpires());
                                editor.commit();
                            }

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

        }
于 2013-01-31T10:09:32.740 回答