2015-07-11 101 views
0

我跟着this指南將Facebook登錄按鈕添加到我的Android項目中,似乎Facebook Studio方法無法被Android Studio識別。例如,對於MainActivity.java文件中的任何Facebook方法,我都會收到警告消息「無法解析符號'TextView'」,並且我收到的所有針對MainActivity.java的Gradle生成錯誤似乎都會出現從導入Facebook SDK失敗。我真的不明白我做錯了什麼;看下面的代碼。爲什麼Facebook SDK無法導入我的Android項目?

的MainActivity.java文件,其中使用來自Facebook的SDK導入方法:

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

應用程序級的build.gradle文件

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'; 
} 

搖籃控制檯錯誤在該項目試圖建立日誌:

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. 

感謝您的幫助。

回答

1

發生錯誤的行(第50行)不在方法內,該方法無效。您應該將所有用於登錄按鈕回調的代碼放在onCreate方法中。

相關問題