简体   繁体   中英

Login With Facebook on Android

I'm trying to add 'Login With Facebook' Feature to my app and I'm facing the following Issues

  1. When I add Facebook Login button to XML, I get rendering issues(which goes away when I refresh the Layout, but again there appears a White screen like Below) Code:
    <TextView android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="31dp" android:layout_gravity="center" android:layout_marginLeft="175dp" android:text="Login With Facebook" android:backgroundTint="#948b8b" /> <com.facebook.login.widget.LoginButton android:id="@+id/loginButton" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

Screen Rendering

  1. When I try to Run my App, I get the below Stack Trace in Console.

I'm all new to Android and Couldn't solve the issues. Your Help is much appreciated. Code:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); if(PrefUtils.getCurrentUser(LoginActivity.this) != null){ Intent homeIntent = new Intent(LoginActivity.this, MainActivity.class); startActivity(homeIntent); finish(); } }

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.batman.eventmanager, PID: 2111 java.lang.ExceptionInInitializerError at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:288) at android.view.LayoutInflater.createView(LayoutInflater.java:607) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at android.view.LayoutInflater.inflate(LayoutInflater.java:365) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378) at android.app.Activity.setContentView(Activity.java:2145) at com.example.batman.eventmanager.LoginActivity.onCreate(LoginActivity.java:33) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: null at com.facebook.internal.Validate.sdkInitialized(Validate.java:99) at com.facebook.FacebookSdk.getCallbackRequestCodeOffset(FacebookSdk.java:735) at com.facebook.internal.CallbackManagerImpl$RequestCodeOffset.toRequestCode(CallbackManagerImpl.java:109) at com.facebook.login.widget.LoginButton.<clinit>(LoginButton.java:58) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:288) at android.view.LayoutInflater.createView(LayoutInflater.java:607) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at android.view.LayoutInflater.inflate(LayoutInflater.java:365) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378) at android.app.Activity.setContentView(Activity.java:2145) at com.example.batman.eventmanager.LoginActivity.onCreate(LoginActivity.java:33) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387 at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

  1. Below is my Manifest XML `

     <activity android:name=".LoginActivity" 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=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="com.example.batman.eventmanager.MainActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <uses-permission android:name="android.permission.INTERNET"/> <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/> <activity android:name="com.facebook.FacebookActivity" android:configChanges= "keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:label="@string/app_name" /> 

    ` TIA

You need to initialize the Facebook sdk before setContentView()

put below code before setContentView();

       FacebookSdk.sdkInitialize(getApplicationContext());
       callbackManager = CallbackManager.Factory.create();

java.lang.ExceptionInInitializerError at java.lang.reflect.Constructor.newInstance(Native Method) shows that you didn't initialize the facebook SDK. Do the following to resolve this exception.

public class LoginActivity extends FragmentActivity {

    private ConnectionDetector cd;
    LoginFragment loginFragment;      

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            FacebookSdk.sdkInitialize(this.getApplicationContext());
            if (savedInstanceState == null) {

                    loginFragment = new LoginFragment();
                    getSupportFragmentManager()
                            .beginTransaction()
                            .add(android.R.id.content, loginFragment)
                            .commit();
                } else {

                    loginFragment = (LoginFragment) getSupportFragmentManager()
                            .findFragmentById(android.R.id.content);
                }
            }

After that take a new fragment LoginFragment and add all the functionality here:

public class LoginFragment extends android.support.v4.app.Fragment {

    LoginButton loginButton;
    CallbackManager callbackManager;

    public LoginFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        callbackManager = CallbackManager.Factory.create();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootview= inflater.inflate(R.layout.activity_login, container, false);

        loginButton = (LoginButton) rootview.findViewById(R.id.login_button);
        loginButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        loginButton.setReadPermissions("public_profile", "email", "user_about_me");
        // If using in a fragment
        loginButton.setFragment(this);

          }
}   

You need to use FaceBook SDK to enable facebook login. The developer portal of facebook has all the details you are looking for. Please spend time understanding the steps.

Details at, https://developers.facebook.com/docs/facebook-login/android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM