Javascript Facebook登录android应用程序

Javascript Facebook登录android应用程序,javascript,android,xml,facebook,facebook-graph-api,Javascript,Android,Xml,Facebook,Facebook Graph Api,大家好,我正试图在我的android应用程序中登录Facebook,我想将Facebook用户名和Facebook id传递给另一个活动,但我不知道怎么做。无论如何,我的登录不起作用,我的应用程序在我打开时关闭。以下是我的活动: import android.content.Context; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import androi

大家好,我正试图在我的android应用程序中登录Facebook,我想将Facebook用户名和Facebook id传递给另一个活动,但我不知道怎么做。无论如何,我的登录不起作用,我的应用程序在我打开时关闭。以下是我的活动:

import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;


public class LoginActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    /**
     * A placeholder fragment containing a simple view.
     */
    public class PlaceholderFragment extends Fragment {

        CallbackManager callbackManager;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            FacebookSdk.sdkInitialize(getApplicationContext());

            callbackManager = CallbackManager.Factory.create();

            LoginManager.getInstance().registerCallback(callbackManager,
                    new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {
                            // App code
                        }

                        @Override
                        public void onCancel() {
                            // App code
                        }

                        @Override
                        public void onError(FacebookException exception) {
                            // App code
                        }
                    });
        }
        public PlaceholderFragment() {
        }

        public View onCreateView(
                LayoutInflater inflater,
                ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.activity_login, container, false);

            LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
            loginButton.setReadPermissions("user_friends");
            // If using in a fragment
            loginButton.setFragment(this);
            // Other app specific specialization

            // Callback registration
            CallbackManager callbackManager = null;
            loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    // App code
                }

                @Override
                public void onCancel() {
                    // App code
                }

                @Override
                public void onError(FacebookException exception) {
                    // App code
                }
            });

            return view;

        }


    }
}

我做了一个简单的例子,可以正确地工作

AndroidMenifest.xml

?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tekinarslan.material.sample" >

    <uses-permission android:name="android.permission.INTERNET" />

    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

        <activity
            android:name=".SampleActivity"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
<uses-permission android:name="android.permission.INTERNET"/>

<application
        android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
</application>
MyActivity.java

public class MainActivity extends Activity {

    private CallbackManager callbackManager;
    private ProfileTracker profileTracker;
    private Button loginButton;
    private TextView textViewName;
    private ProfilePictureView profilePictureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        callbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                updateProfile();
            }

            @Override
            public void onCancel() {
                updateProfile();
            }

            @Override
            public void onError(FacebookException e) {
                updateProfile();
            }
        });

        textViewName = (TextView) findViewById(R.id.textViewName);
        profilePictureView = (ProfilePictureView) findViewById(R.id.profilePictureView);
        loginButton = (Button) findViewById(R.id.buttonLogin);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AccessToken token = AccessToken.getCurrentAccessToken();
                if (token == null) {
                    LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "user_friends", "email"));
                } else {
                    LoginManager.getInstance().logOut();
                }
            }
        });

        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile profile, Profile profile1) {
                updateProfile();
            }
        };
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    private void updateProfile() {
        Profile profile = Profile.getCurrentProfile();
        if (profile != null) {
            profilePictureView.setProfileId(profile.getId());
            textViewName.setText(profile.getName());
            loginButton.setText("Logout");

        } else {
            profilePictureView.setProfileId(null);
            textViewName.setText("not login yet");
            loginButton.setText("Login");
        }
    }

}
公共类MainActivity扩展活动{
私人CallbackManager CallbackManager;
个人档案跟踪器;
私人按钮登录按钮;
私有文本视图文本视图名称;
私人档案PictureView档案PictureView;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callbackManager=callbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback()){
@凌驾
成功时公共无效(LoginResult LoginResult){
updateProfile();
}
@凌驾
公开作废{
updateProfile();
}
@凌驾
公共无效onError(FaceBook例外e){
updateProfile();
}
});
textViewName=(TextView)findViewById(R.id.textViewName);
profilePictureView=(profilePictureView)findViewById(R.id.profilePictureView);
loginButton=(按钮)findViewById(R.id.buttonLogin);
loginButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
AccessToken token=AccessToken.getCurrentAccessToken();
if(标记==null){
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this、Arrays.asList(“公共配置文件”、“用户朋友”、“电子邮件”);
}否则{
LoginManager.getInstance().logOut();
}
}
});
profileTracker=新的profileTracker(){
@凌驾
更改当前配置文件时受保护的无效(配置文件配置文件,配置文件1){
updateProfile();
}
};
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
callbackManager.onActivityResult(请求代码、结果代码、数据);
}
私有void updateProfile(){
Profile Profile=Profile.getCurrentProfile();
if(profile!=null){
profilePictureView.setProfileId(profile.getId());
textViewName.setText(profile.getName());
loginButton.setText(“注销”);
}否则{
profilePictureView.setProfileId(空);
textViewName.setText(“尚未登录”);
setText(“登录”);
}
}
}
activity_main.xml

    <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"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:paddingBottom="@dimen/activity_vertical_margin"
              tools:context=".MainActivity">

    <Button
            android:id="@+id/buttonLogin"
            android:text="Login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>


    <com.facebook.login.widget.ProfilePictureView
            android:id="@+id/profilePictureView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    <TextView
            android:id="@+id/textViewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>


logcat中是否有任何错误消息告诉您登录失败?请查看我的编辑@LeoLinit无论如何都不起作用,logcat是相同的,我尝试解决此问题的时间是2周,请帮助我做到这一点,我将把整个项目发布到这里:@LeoLinI发现了一些您可能遗漏的内容,因此我用一个简单的示例编辑了我的答案。现在它给了我以下错误:05-08 15:27:16.894 23498-23498/com.tekinarslan.material.sample E/AndroidRuntime﹕ 致命异常:主java.lang.RuntimeException:无法启动活动组件信息{com.tekinarslan.material.sample/com.tekinarslan.material.sample.LoginActivity}:java.lang.ClassCastException:android.widget.Button无法转换为android.app.ActivityThread.performLaunchActivity上的com.tekinarslan.material.sample.Button(ActivityThread.java:2295)@LeoLinIt的java.lang.ClassCastException,您是否在xml中使用了自定义按钮?
public class MyApplication extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        FacebookSdk.sdkInitialize(this);
    }
}
public class MainActivity extends Activity {

    private CallbackManager callbackManager;
    private ProfileTracker profileTracker;
    private Button loginButton;
    private TextView textViewName;
    private ProfilePictureView profilePictureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        callbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                updateProfile();
            }

            @Override
            public void onCancel() {
                updateProfile();
            }

            @Override
            public void onError(FacebookException e) {
                updateProfile();
            }
        });

        textViewName = (TextView) findViewById(R.id.textViewName);
        profilePictureView = (ProfilePictureView) findViewById(R.id.profilePictureView);
        loginButton = (Button) findViewById(R.id.buttonLogin);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AccessToken token = AccessToken.getCurrentAccessToken();
                if (token == null) {
                    LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "user_friends", "email"));
                } else {
                    LoginManager.getInstance().logOut();
                }
            }
        });

        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile profile, Profile profile1) {
                updateProfile();
            }
        };
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    private void updateProfile() {
        Profile profile = Profile.getCurrentProfile();
        if (profile != null) {
            profilePictureView.setProfileId(profile.getId());
            textViewName.setText(profile.getName());
            loginButton.setText("Logout");

        } else {
            profilePictureView.setProfileId(null);
            textViewName.setText("not login yet");
            loginButton.setText("Login");
        }
    }

}
    <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"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:paddingBottom="@dimen/activity_vertical_margin"
              tools:context=".MainActivity">

    <Button
            android:id="@+id/buttonLogin"
            android:text="Login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>


    <com.facebook.login.widget.ProfilePictureView
            android:id="@+id/profilePictureView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    <TextView
            android:id="@+id/textViewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>