使用Facebook登录按钮

时间:2015-07-27 05:47:13

标签: android facebook facebook-sdk-4.0

我在使用Android studio创建facebook登录按钮时遇到问题。以下是我的文件。我想在我的应用程序中使用Facebook登录功能。代码运行正常,但我收到了在此消息结束时提到的错误。

MainActivity.java

package com.example.rvaja.newapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 MainActivity extends Activity {
    private TextView info;
    private LoginButton loginButton;
    private  CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FacebookSdk.sdkInitialize(getApplicationContext());
        info = (TextView)findViewById(R.id.info);
        callbackManager = CallbackManager.Factory.create();
        loginButton = (LoginButton)findViewById(R.id.login_button);
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                info.setText(
                        "User ID: "
                                + loginResult.getAccessToken().getUserId()
                                + "\n" +
                                "Auth Token: "
                                + loginResult.getAccessToken().getToken()
                );
            }

            @Override
            public void onCancel() {
                info.setText("Login attempt canceled.");
            }

            @Override
            public void onError(FacebookException e) {
                info.setText("Login attempt failed.");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml中

<RelativeLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/info"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="18sp"
        />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</RelativeLayout>

**AndroidManifest.xml**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rvaja.newapp" >

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

        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/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:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name" />
    </application>

</manifest>

构建gradle(模块:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "22.0.1"

    repositories {
        mavenCentral()
    }

    defaultConfig {
        applicationId "com.example.rvaja.newapp"
        minSdkVersion 21
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.facebook.android:facebook-android-sdk:4.1.0'
}


I am getting the following error and also in the emulator I get an error message saying 
  

&#34;很遗憾,您的应用已停止

     

&#34;渲染问题无法实例化以下类:

     

com.facebook.login.widget.LoginButton(Open Class,Show Exception,Clear Cache)提示:在自定义视图中使用View.isInEditMode()跳过   在IDE异常详细信息中显示代码或显示示例数据   java.lang.NoClassDefFoundError:无法初始化类   com.facebook.login.widget.LoginButton at   java.lang.reflect.Constructor.newInstance(Constructor.java:513)at   android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:806)   在   android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)   在android.view.LayoutInflater.rInflate(LayoutInflater.java:782)at   android.view.LayoutInflater.inflate(LayoutInflater.java:504)at   android.view.LayoutInflater.inflate(LayoutInflater.java:385)复制   堆叠到剪贴板&#34;

1 个答案:

答案 0 :(得分:1)

在MainActivity类中, FacebookSdk.sdkInitialize(getApplicationContext()); 在调用setContentView(R.layout.activity_main);

之前放置这一行

活动 - 在setContentView(R.layout.activity_main)之前

片段 - 在inflater.inflate之前(R.layout.fragment_main,container,false)