2016-03-04 107 views
0

我已经按照此facebook .. link为集成,但我的应用程序显示空白活动。当我运行我的应用程序时,Facebook登录按钮不显示。与android的Facebook集成不起作用

我有developer.facebook.com完整的应用注册使用的密钥和APP_ID

然后我有以下的代码做....

AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    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" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <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" 
     android:theme="@android:style/Theme.Translucent.NoTitleBar" /> 
</application> 

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
tools:layout="@layout/fragment_simple_login_button" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

FragmentSimpleLoginButton

<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" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".FragmentSimpleLoginButton"> 

<TextView 
    android:id="@+id/text_details" 
    android:layout_width="match_parent" 
    android:gravity="center" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_above="@+id/login_button" /> 

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

MainActivity

public class MainActivity extends FragmentActivity { 

public static final int INDEX_SIMPLE_LOGIN = 0; 
public static final int INDEX_CUSTOM_LOGIN = 1; 

private static final String STATE_SELECTED_FRAGMENT_INDEX = "selected_fragment_index"; 
public static final String FRAGMENT_TAG = "fragment_tag"; 
private FragmentManager mFragmentManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mFragmentManager = getSupportFragmentManager(); 

} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
} 

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

    if (id == R.id.login_button) { 
     toggleFragment(INDEX_SIMPLE_LOGIN); 
     return true; 
    } 
    if (id == R.id.login_button) { 
     toggleFragment(INDEX_CUSTOM_LOGIN); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

private void toggleFragment(int index) { 
    Fragment fragment = mFragmentManager.findFragmentByTag(FRAGMENT_TAG); 
    FragmentTransaction transaction = mFragmentManager.beginTransaction(); 
    switch (index){ 
     case INDEX_SIMPLE_LOGIN: 
      transaction.replace(android.R.id.content, new FragmentSimpleLoginButton(),FRAGMENT_TAG); 
      break; 
     case INDEX_CUSTOM_LOGIN: 
      transaction.replace(android.R.id.content, new FragmentCustomLoginButton(),FRAGMENT_TAG); 
      break; 
    } 
    transaction.commit(); 
} 

}

FragmentSimpleLoginButton

public class FragmentSimpleLoginButton extends Fragment { 

private TextView mTextDetails; 
private CallbackManager mCallbackManager; 
private AccessTokenTracker mTokenTracker; 
private ProfileTracker mProfileTracker; 
private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() { 
    @Override 
    public void onSuccess(LoginResult loginResult) { 
     Log.d("Vvvvvvv", "onSuccess"); 
     AccessToken accessToken = loginResult.getAccessToken(); 
     Profile profile = Profile.getCurrentProfile(); 
     mTextDetails.setText(constructWelcomeMessage(profile)); 

    } 


    @Override 
    public void onCancel() { 
     Log.d("Vvvvvvvv", "onCancel"); 
    } 

    @Override 
    public void onError(FacebookException e) { 
     Log.d("Vvvvvvvv", "onError " + e); 
    } 
}; 


public FragmentSimpleLoginButton() { 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    mCallbackManager = CallbackManager.Factory.create(); 
    setupTokenTracker(); 
    setupProfileTracker(); 

    mTokenTracker.startTracking(); 
    mProfileTracker.startTracking(); 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_simple_login_button, container, false); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    setupTextDetails(view); 
    setupLoginButton(view); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    Profile profile = Profile.getCurrentProfile(); 
    mTextDetails.setText(constructWelcomeMessage(profile)); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    mTokenTracker.stopTracking(); 
    mProfileTracker.stopTracking(); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    mCallbackManager.onActivityResult(requestCode, resultCode, data); 
} 

private void setupTextDetails(View view) { 
    mTextDetails = (TextView) view.findViewById(R.id.text_details); 
} 

private void setupTokenTracker() { 
    mTokenTracker = new AccessTokenTracker() { 
     @Override 
     protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) { 
      Log.d("Vvvvvvv", "" + currentAccessToken); 
     } 
    }; 
} 

private void setupProfileTracker() { 
    mProfileTracker = new ProfileTracker() { 
     @Override 
     protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) { 
      Log.d("Vvvvvvvv", "" + currentProfile); 
      mTextDetails.setText(constructWelcomeMessage(currentProfile)); 
     } 
    }; 
} 

private void setupLoginButton(View view) { 
    LoginButton mButtonLogin = (LoginButton) view.findViewById(R.id.login_button); 
    mButtonLogin.setFragment(this); 
    mButtonLogin.setReadPermissions("user_friends"); 
    mButtonLogin.registerCallback(mCallbackManager, mFacebookCallback); 
} 

private String constructWelcomeMessage(Profile profile) { 
    StringBuffer stringBuffer = new StringBuffer(); 
    if (profile != null) { 
     stringBuffer.append("Welcome " + profile.getName()); 
    } 
    return stringBuffer.toString(); 
} 

}

error in logcat

请帮我... 谢谢!

回答

0

一切都很好,但你错过了你的活动中的FB按钮。为什么你不添加以下代码

<com.facebook.login.widget.LoginButton 
    android:id="@+id/login_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="30dp" 
    android:layout_marginBottom="30dp" /> 
0

这些许可,需要这样的清单文件里面,你没有指定Provider内部清单文件的话,看看下面的例子,在你结束纠正: -

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

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

     <provider 
      android:name="com.facebook.FacebookContentProvider" 
      android:authorities="com.facebook.app.FacebookContentProvider16554302080646"//// PROVIDE YOURS IDS HERE 
      android:exported="true" /> 
+0

我有我的应用程序ID,但仍当我运行程序的时候,是空白的活动没有Facebook的按钮,会出现添加此供应商的标签.. –

+0

@VandanaRao ..DId你给正确的Facebook ID在的结束“ android:authorities“??首先检查一下 –

+0

我已经添加了这个样子....... android:authorities =”com.facebook.app.FacebookConte ntProvider539236509587640“ –