简体   繁体   中英

Why is the Facebook SDK failing to import for my Android Project?

I followed this guide for adding the Facebook login button to my Android project, and it seems the Facebook methods aren't recognized by Android Studio. For instance, for any of the Facebook methods I have in the MainActivity.java file, I am given the warning message "Cannot resolve symbol 'TextView'", and all of the Gradle Build Errors I have received for MainActivity.java seem to come from the failure to import the Facebook SDK. I really can't see what it is that I am doing incorrectly; see code below.

The MainActivity.java file where methods from the Facebook SDK import are used:

package com.checkit.ben.checkit;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

    private TextView info;
    private LoginButton loginbutton;
    private CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        com.facebook.FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        callbackManager = CallbackManager.Factory.create();
        info = (TextView)findViewById(R.id.info);
        loginButton = (LoginButton)findViewById(R.id.login_button);
    }

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

    @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
    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);
    }

    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.");
        }
    }
}

The application level build.gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.checkit.ben.checkit"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    mavenCentral();
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar']);
    compile 'com.android.support:appcompat-v7:22.1.1';
    compile 'com.facebook.android:facebook-android-sdk:4.3.0';
}

Gradle Console Error Log upon attempted build of the project:

Executing tasks: [clean, :app:compileDebugSources, :app:compileDebugAndroidTestSources]

Configuration on demand is an incubating feature.
:app:clean
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72211Library
:app:prepareComAndroidSupportSupportV42211Library
:app:prepareComFacebookAndroidFacebookAndroidSdk430Library
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJava
/Users/ben/AndroidStudioProjects/CheckIt/app/src/main/java/com/checkit/ben/checkit/MainActivity.java:50: error: <identifier> expected
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                                ^
/Users/ben/AndroidStudioProjects/CheckIt/app/src/main/java/com/checkit/ben/checkit/MainActivity.java:50: error: <identifier> expected
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                                                ^
/Users/ben/AndroidStudioProjects/CheckIt/app/src/main/java/com/checkit/ben/checkit/MainActivity.java:50: error: illegal start of type
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                                                  ^
/Users/ben/AndroidStudioProjects/CheckIt/app/src/main/java/com/checkit/ben/checkit/MainActivity.java:50: error: ')' expected
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                                                     ^
/Users/ben/AndroidStudioProjects/CheckIt/app/src/main/java/com/checkit/ben/checkit/MainActivity.java:50: error: ';' expected
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                                                                      ^
/Users/ben/AndroidStudioProjects/CheckIt/app/src/main/java/com/checkit/ben/checkit/MainActivity.java:50: error: <identifier> expected
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                                                                                  ^
/Users/ben/AndroidStudioProjects/CheckIt/app/src/main/java/com/checkit/ben/checkit/MainActivity.java:50: error: illegal start of type
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                                                                                   ^
/Users/ben/AndroidStudioProjects/CheckIt/app/src/main/java/com/checkit/ben/checkit/MainActivity.java:50: error: <identifier> expected
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                                                                                    ^
/Users/ben/AndroidStudioProjects/CheckIt/app/src/main/java/com/checkit/ben/checkit/MainActivity.java:50: error: ';' expected
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                                                                                     ^
/Users/ben/AndroidStudioProjects/CheckIt/app/src/main/java/com/checkit/ben/checkit/MainActivity.java:79: error: class, interface, or enum expected
}
^
10 errors

 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Thank you for your assistance.

The line on which the error is occurring (line 50) is not within a method, which is invalid. You should probably put all the code for registering the callback for the login button in your onCreate method.

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