我问facebook android sdk身份验证错误。
当我打开程序时,出现错误的是“已意外停止。请再试一次。”
debuge错误是关于NullPointerException问题。 (facebook-android-sdk / facebook / src / com / facebook / internal / Validate.java)
我该如何解决这个问题?感谢。
package com.example.fbloginau;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
public class MainFragment extends FragmentActivity
{
private Fragment Fragment;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
Fragment = new Fragment();
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, Fragment)
.commit();
}
else
{
// Or set the fragment from restored state info
Fragment = (Fragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
}
}
块引用
<LinearLayout 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:orientation="vertical">
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
/>
块引用
enter code here
答案 0 :(得分:1)
你可以使用社交auth sdk进行android facebook集成,你可以在这里找到代码:
http://code.google.com/p/socialauth-android/downloads/list
http://www.codeproject.com/Tips/457153/How-to-integrate-Facebook-Twitter-Linkedin-in-Andr
答案 1 :(得分:0)
您可以尝试通过某些Web服务获取访问令牌并进行初始化。 您可以在活动中授权您的会话并通过活页夹传递它。 Facebook facebook
这里的例子。
public class ClientService extends Service {
//Facebook object to operate from service
private Facebook facebook;
...
private final ClientBinder clientBinter = new ClientBinder();
/** Binder to service */
public class ClientBinder extends Binder {
public ClientService getService() {
return ClientService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return clientBinter;
}
public void setFacebook(Facebook f) {
ClientService.this.facebook = f;
}
public boolean loggedFacebook() {
if (facebook == null)
return false;
return facebook.isSessionValid();
}
}
public class DemoActivity extends Activity{
private ClientService clientService;
private ServiceConnection clientConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
public void onServiceConnected(ComponentName name, IBinder service) {
ClientService.ClientBinder binder = (ClientService.ClientBinder) service;
clientService = binder.getService();
mBound = true;
if (facebook != null)
if (facebook.isSessionValid())
clientService.setFacebook(facebook);
}
};
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, ClientService.class);
bindService(intent, clientConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if (mBound) {
unbindService(clientConnection);
mBound = false;
}
}
private void logINFacebook() {
if (facebook.isSessionValid())
return;
facebook.authorize(this,
getResources().getStringArray(R.array.facebookPermissions),
new DialogListener() {
public void onFacebookError(FacebookError e) {...}
public void onError(DialogError e) {...}
public void onComplete(Bundle values) {
if (mBound) {
clientService.setFacebook(facebook);
}
//Save to shared preferences(optional)
SharedPreferences.Editor editor = prefs.edit();
editor.putString(FACEBOOK_TOKEN_PREFNAME,
facebook.getAccessToken());
editor.putLong(FACEBOOK_EXPIRES_PREFNAME,
facebook.getAccessExpires());
editor.commit();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
facebook.authorizeCallback(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
}