2

我是安卓新手。我已经在我的应用程序中集成了 facebook,它可以正常工作。当我想分享我的内容时,突然出现上述错误。如何解决这个问题请帮助我。

提前谢谢

我的 faceboobc.java 类代码如下

import com.example.shareslabfb.DialogError;
import com.example.shareslabfb.Facebook;
import com.example.shareslabfb.Facebook.DialogListener;
import com.example.shareslabfb.FacebookError;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;

import android.view.View;
import android.view.Window;
import android.widget.Toast;

public class Facebookc extends Activity{

    private static final String APP_ID = ".............";
    private static final String[] PERMISSIONS = new String[] {"publish_stream"};

    private static final String TOKEN = "access_token";
        private static final String EXPIRES = "expires_in";
        private static final String KEY = "facebook-credentials";

    private Facebook facebook;
    private String messageToPost;


    public boolean saveCredentials(Facebook facebook) {
        Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putString(TOKEN, facebook.getAccessToken());
        editor.putLong(EXPIRES, facebook.getAccessExpires());
        return editor.commit();
    }

    public boolean restoreCredentials(Facebook facebook) {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
        facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
        facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
        return facebook.isSessionValid();
    }

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

    facebook = new Facebook(APP_ID);
    restoreCredentials(facebook);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.facebook_dialog);


    String facebookMessage = getIntent().getStringExtra("facebookMessage");
    if (facebookMessage == null){
        /*if(!MessageList.URLToPost.isEmpty()){
            System.out.println("Inside IF");
            String title="  . For more info Click : ";
            //facebookMessage = "";
            facebookMessage = MessageList.title;
        }else{System.out.println("Inside ELSE");*/
        facebookMessage = "";


    }
    messageToPost = facebookMessage;
}

public void doNotShare(View button){
    finish();
}
public void share(View button){
    if (! facebook.isSessionValid()) {
        loginAndPostToWall();
    }
    else {
        postToWall(messageToPost);
    }
}

public void loginAndPostToWall(){
     facebook.authorize(this, PERMISSIONS,Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}

public void postToWall(String message){
    Bundle parameters = new Bundle();
    String s;
    if(MessageList.singleDescription.length()>70){
        s=MessageList.singleDescription.substring(0, 70);   
    }else{
        s=" ";
    }

    parameters.putString("title", message);
    parameters.putString("description", s);
    parameters.putString("picture", MessageList.imageURL);
    parameters.putString("link", MessageList.URLToPost); 
        facebook.dialog(this, "stream.publish", parameters, new WallPostDialogListener());
}

class LoginDialogListener implements DialogListener {
    public void onComplete(Bundle values) {
        saveCredentials(facebook);
        if (messageToPost != null){
        postToWall(messageToPost);
    }
    }
    public void onFacebookError(FacebookError error) {
        showToast("Authentication with Facebook failed!");
        finish();
    }
    public void onError(DialogError error) {
        showToast("Authentication with Facebook failed!");
        finish();
    }
    public void onCancel() {
        showToast("Authentication with Facebook cancelled!");
        finish();
    }
}

class WallPostDialogListener implements DialogListener {
    public void onComplete(Bundle values) {
                final String postId = values.getString("post_id");
                if (postId != null) {
                showToast("Message posted to your facebook wall!");
            } else {
                showToast("Wall post cancelled!");
            }
            finish();
        }
    public void onFacebookError(FacebookError e) {
        showToast("Failed to post to wall!");
        e.printStackTrace();
        finish();
    }
    public void onError(DialogError e) {
        showToast("Failed to post to wall!");
        e.printStackTrace();
        finish();
    }
    public void onCancel() {
        showToast("Wall post cancelled!");
        finish();
    }
    }

private void showToast(String message){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

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

    facebook.authorizeCallback(requestCode, resultCode, data);
}
}
4

1 回答 1

1

在这里,您正在使用 Facebook Dialog 将 Facebook 集成到您的 android 应用程序中......所以,有时会发生这种情况......

使用此链接将 facebook 集成到您的应用程序中。

http://developers.facebook.com/docs/mobile/android/hackbook/

于 2012-11-22T09:37:29.610 回答