通过Facebook会话连接到Android应用程序时出错

时间:2013-12-15 22:54:00

标签: android eclipse android-facebook

我从Github获得了此代码。我按照所有指示进行了操作,并且MainActivity中出现了一个我似乎无法解决的错误。

以下是代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.facebook.LoggingBehavior;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Settings;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupFacebookConnect(savedInstanceState);

Button facebook_connect = (Button) findViewById(R.id.facebookLoginButton);
facebook_connect.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
 if (isFacebookConnected()) {
    disConnectFacebook();
} else {
connectFacebook();
}

}
});
}

private Session.StatusCallback statusCallback = new FBSessionStatus();

public void setupFacebookConnect(Bundle savedInstanceState) {
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

Session session = Session.getActiveSession();
if (session == null) {
 if (savedInstanceState != null) {
   session = Session.restoreSession(this, null, statusCallback,
    savedInstanceState);
 }
if (session == null) {
session = new Session(this);
} 
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this)
.setCallback(statusCallback));
}
}
}

public boolean isFacebookConnected() {
Session session = Session.getActiveSession();
return (session.isOpened()) ? true : false;
}

public void connectFacebook() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this)
.setCallback(statusCallback));
} else {
Session.openActiveSession(this, true, statusCallback);
} 
}

@Override
public void onStart() {
 super.onStart();
  Session.getActiveSession().addCallback(statusCallback);
 }

@Override
public void onStop() {
 super.onStop();
 Session.getActiveSession().removeCallback(statusCallback);
 }

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}

public class FBSessionStatus implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state,
Exception exception) {

}
}

public void disConnectFacebook() {
Session session = Session.getActiveSession();
 if (!session.isClosed()) {
   session.closeAndClearTokenInformation();
 }
 }

 }

以下是错误所在的部分:

enter image description here

只是因为你无法阅读它,它说

  

Android.support.v4.app.Fragment类型无法解析。它是从必需的.class文件间接引用的

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您必须将Android v4支持库添加到项目中。看看here,了解如何设置。