添加没有片段的Facebook登录按钮

时间:2017-09-28 06:37:31

标签: android facebook

在查看了多个教程,文档和其他SO主题之后,我仍然无法解决这个返回的问题。添加LoginButton本身后,应用程序只会在打开时崩溃而没有明确的错误消息(除非我只是忽略它)。这是我目前的设置:

AndroidManifest

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

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

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

            <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>    
            <activity android:name=".Home">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

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

主页

    package com.theindiestation.carmeetz;

    import android.graphics.Typeface;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.TextView;

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

    public class Home extends AppCompatActivity {

        private LoginButton loginButton;
        private CallbackManager callbackManager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.activity_home);

            Typeface font = Typeface.createFromAsset(getAssets(), "fonts/hometitle.ttf");

            TextView homeTitle = (TextView) findViewById(R.id.homeTitle);
            homeTitle.setTypeface(font);
            TextView homeText = (TextView)findViewById(R.id.homeText);
            homeText.setTypeface(font);
            TextView homeFooter = (TextView)findViewById(R.id.homeFooter);
            homeFooter.setTypeface(font);

            FacebookSdk.sdkInitialize(getApplicationContext());

            callbackManager = CallbackManager.Factory.create();
            loginButton = (LoginButton)findViewById(R.id.login_button);

            loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException e) {

            }
        });
    }
}

activity_home

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.theindiestation.carmeetz.Home"
    android:background="@drawable/background_1">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/homeTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="Car Meetz"
        android:gravity="center"
        android:textSize="30dp"
        android:textColor="#fff" />

    <TextView
        android:id="@+id/homeText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:text="@string/home_text"
        android:gravity="center"
        android:textSize="16dp"
        android:textColor="#fff" />


    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/homeText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="96dp" />

    <TextView
        android:id="@+id/homeFooter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="2dp"
        android:text="Some text"
        android:gravity="center"
        android:textSize="12dp"
        android:textColor="#fff" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>

我还准备添加LogCat,但即使在不再触摸我的手机一小时后,它也会充斥着随机的行,包括一条消息“要处理很多输入”,所以这个用处不大我很害怕。

编辑#1 将活动添加到清单文件后,它仍然无法正常工作,但现在我得到一个正确的错误日志,其中包含:

--------- beginning of crash
09-28 15:40:24.195 23155-23155/com.theindiestation.carmeetz E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.theindiestation.carmeetz, PID: 23155
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.theindiestation.carmeetz/com.theindiestation.carmeetz.Home}: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class com.facebook.login.widget.LoginButton
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
                                                                                  at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                                               Caused by: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class com.facebook.login.widget.LoginButton
                                                                               Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class com.facebook.login.widget.LoginButton
                                                                               Caused by: java.lang.reflect.InvocationTargetException
                                                                                  at java.lang.reflect.Constructor.newInstance0(Native Method)
                                                                                  at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
                                                                                  at android.view.LayoutInflater.createView(LayoutInflater.java:652)
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
                                                                                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
                                                                                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
                                                                                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                                  at com.theindiestation.carmeetz.Home.onCreate(Home.java:27)
                                                                                  at android.app.Activity.performCreate(Activity.java:6912)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
                                                                                  at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                                               Caused by: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.
                                                                                  at com.facebook.internal.Validate.sdkInitialized(Validate.java:136)
                                                                                  at com.facebook.AccessTokenTracker.<init>(AccessTokenTracker.java:55)
                                                                                  at com.facebook.login.widget.LoginButton$2.<init>(LoginButton.java:561)
                                                                                  at com.facebook.login.widget.LoginButton.configureButton(LoginButton.java:561)
                                                                                  at com.facebook.FacebookButtonBase.<init>(FacebookButtonBase.java:66)
                                                                                  at com.facebook.login.widget.LoginButton.<init>(LoginButton.java:200)
                                                                                  at java.lang.reflect.Constructor.newInstance0(Native Method) 
                                                                                  at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
                                                                                  at android.view.LayoutInflater.createView(LayoutInflater.java:652) 
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794) 
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734) 
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:865) 
                                                                                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828) 
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:873) 
                                                                                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:525) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:427) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:378) 
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289) 
                                                                                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                                                                                  at com.theindiestation.carmeetz.Home.onCreate(Home.java:27) 
                                                                                  at android.app.Activity.performCreate(Activity.java:6912) 
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) 
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877) 
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985) 
                                                                                  at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6692) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

我已经添加了这样的活动:

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

        <activity android:name=".Home">
            <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" />

    </application>

</manifest>

并尝试使用:

<activity android:name="com.facebook.FacebookActivity"/>

2 个答案:

答案 0 :(得分:2)

在清单文件中添加此活动

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

FacebookSdk.sdkInitialize(getApplicationContext());         的setContentView(R.layout.youractivity);

答案 1 :(得分:0)

好吧,看来我的代码毕竟还好,感谢Chirag Khandla和Nilesh Rathod加入活动。唯一的问题是我必须初始化facebookSDK BEFORE setContentView()。