通过Android应用程序在Facebook上发帖

时间:2014-02-13 11:03:01

标签: android facebook facebook-wall

我是android的新手。我想通过我的应用程序实现facebook墙贴。我已经完成了其他方面但是墙贴未正确完成。当我点击textview然后我的应用程序显示应用程序已停止..请帮助我..

点击类名为..

public void Share(View view)
    {
        Intent share =new Intent(this, SharePerks.class);
        //use.putExtra("perk_name", perk_name);
        //use.putExtra("perk_short_desc", perk_short_desc);
        //use.putExtra("perk_summary_desc", perk_summary_desc);
        startActivity(share);
    }

SharePerks.class

public class SharePerks extends FragmentActivity{

    private Button ShareButton;
    private UiLifecycleHelper uiHelper;
    private Session.StatusCallback callback = 
            new Session.StatusCallback() {
            @Override
            public void call(Session session, 
                    SessionState state, Exception exception) {
                onSessionStateChanged(session, state, exception);
            }
        };
    boolean SharePerksWhenSessionOpened;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.hide();
        setContentView(R.layout.share);

        /*ShareButton = (Button) findViewById(R.id.Share_btn);
        ShareButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                //onClickPickFriends();

                if (ensureOpenSession()) {

                    publishFeedDialog();
                }
                else {
                    SharePerksWhenSessionOpened = true;

                }
            /*}
        });*/
        Log.e("on create", "inside on create");


        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);

       // ensureOpenSession();


    }





    @Override
    protected void onResume() {
        super.onResume();

        uiHelper.onResume();
        AppEventsLogger.activateApp(this);
        Log.e("on resume", "in onresume method");
    }

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

    private boolean ensureOpenSession() 
    {
        if (Session.getActiveSession() != null || Session.getActiveSession().isOpened()) 
        {
            Session.openActiveSession(SharePerks.this, true, new Session.StatusCallback() 
            {
                @Override
                public void call(Session session, SessionState state, Exception exception) 
                {
                    onSessionStateChanged(session, state, exception);

                }
            });
            return false;
        }
        return true;
    }

    private void onSessionStateChanged(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {
            SharePerksWhenSessionOpened = false;

            publishFeedDialog();
        }
    }



    private void publishFeedDialog() {
        Bundle params = new Bundle();
        params.putString("name", "Facebook SDK for Android");
        params.putString("caption", "Build great social apps and get more installs.");
        params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
        params.putString("link", "https://developers.facebook.com/android");
        params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

        WebDialog feedDialog = (
            new WebDialog.FeedDialogBuilder(SharePerks.this,
                Session.getActiveSession(),
                params))
            .setOnCompleteListener(new OnCompleteListener() {

                @Override
                public void onComplete(Bundle values,
                    FacebookException error) {
                    if (error == null) {
                        // When the story is posted, echo the success
                        // and the post Id.
                        final String postId = values.getString("post_id");
                        if (postId != null) {
                            Toast.makeText(SharePerks.this,
                                "Perk Shared",
                                Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            // User clicked the Cancel button
                            Toast.makeText(getApplicationContext(), 
                                "Publish cancelled", 
                                Toast.LENGTH_SHORT).show();
                                finish();
                        }
                    } else if (error instanceof FacebookOperationCanceledException) {
                        // User clicked the "x" button
                        Toast.makeText(getApplicationContext(), 
                            "Publish cancelled", 
                            Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        // Generic, ex: network error
                        Toast.makeText(getApplicationContext(), 
                            "Error posting story", 
                            Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }

            })
            .build();
        feedDialog.show();
    }
}

1 个答案:

答案 0 :(得分:0)

不幸的是我不能发表评论......所以我必须这样做...你能告诉我们你的堆栈跟踪吗?特别是通过Facebook功能,日志非常有用。在那里你也可以找到问题的提示。

否则这对android facebook sdk初学者非常有帮助:Hackbook