Android - 如何正确实施Facebook Share?

时间:2015-03-19 11:36:01

标签: android facebook facebook-graph-api

所以我整个上午都试图在我的应用程序中实现一个Facebook共享按钮,而且我还没有完全开始工作。

我已尝试按照此指南操作:https://developers.facebook.com/docs/android/share

" getActivity()" (其中5个,在onComplete中)方法给出错误"无法解决方法' getActivity()'而我现在迷失了。我尝试过标记与Facebook共享有关的所有内容,以便更容易阅读。我可以补充一点,在添加共享功能之前一切正常。我可能已将代码放在错误的位置或误解了指南,因此我们将非常感谢任何其他指导。

Results.java

package com.example.xxx.zzz;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

//START: FACEBOOK - SHARE
import com.facebook.FacebookException;
import com.facebook.FacebookOperationCanceledException;
import com.facebook.Session;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.FacebookDialog;
import com.facebook.widget.WebDialog;
//END: FACEBOOK - SHARE

public class Results extends FragmentActivity {

    //START: FACEBOOK - SHARE
    private UiLifecycleHelper uiHelper;
    //END: FACEBOOK - SHARE

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

        //START: FACEBOOK - SHARE
        uiHelper = new UiLifecycleHelper(this, null);
        uiHelper.onCreate(savedInstanceState);
        //END: FACEBOOK - SHARE

        themeUtils.onActivityCreateSetTheme(this);
        setContentView(R.layout.activity_results);

        Bundle BMIData = getIntent().getExtras();
        if (BMIData == null) {
            return;
        }

        String BMIMessage = BMIData.getString("BMIMessage");
        String BMIText = BMIData.getString("BMIText");
        final TextView ResultBMITextView = (TextView) findViewById(R.id.ResultBMITextView);
        final TextView ResultBMITextTextView = (TextView) findViewById(R.id.ResultBMITextTextView);
        ResultBMITextView.setText(BMIMessage);
        ResultBMITextTextView.setText(BMIText);
    }

    public void onClickResultsBack(View view) {
        Intent i1 = new Intent(this, MainActivity.class);
        i1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i1);
        finish();
    }

    //START: FACEBOOK - SHARE
    public void onClickShare() {
        if (FacebookDialog.canPresentShareDialog(getApplicationContext(),
                FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
            FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
                    .setLink("https://developers.facebook.com/android")
                    .build();
            uiHelper.trackPendingDialogCall(shareDialog.present());
        } else {
            FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
                    .setLink("https://developers.facebook.com/android")
                    .build();
            uiHelper.trackPendingDialogCall(shareDialog.present());
        }
        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(getActivity(),
                        Session.getActiveSession(),
                        params))
                .setOnCompleteListener(new WebDialog.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(getActivity(),
                                        "Posted story, id: " + postId,
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                // User clicked the Cancel button
                                Toast.makeText(getActivity().getApplicationContext(),
                                        "Publish cancelled",
                                        Toast.LENGTH_SHORT).show();
                            }
                        } else if (error instanceof FacebookOperationCanceledException) {
                            // User clicked the "x" button
                            Toast.makeText(getActivity().getApplicationContext(),
                                    "Publish cancelled",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            // Generic, ex: network error
                            Toast.makeText(getActivity().getApplicationContext(),
                                    "Error posting story",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .build();
        feedDialog.show();
    }

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

        uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
            @Override
            public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
                Log.e("Activity", String.format("Error: %s", error.toString()));
            }

            @Override
            public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
                Log.i("Activity", "Success!");
            }
        });
    }

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

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

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }
    //END: FACEBOOK - SHARE

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_u, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_results.xml

<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="com.example.xxx.zzz.Results">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ResultsBackButtonString"
        android:id="@+id/ResultsBackButton"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="onClickResultsBack"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="BMI: "
        android:id="@+id/ResultTitleTextView"
        android:layout_alignParentTop="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/ResultBMITextView"
        android:text="22.5"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/ResultTitleTextView"
        android:layout_toEndOf="@+id/ResultTitleTextView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/ResultBMITextTextView"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/ResultTitleTextView"
        android:layout_alignLeft="@+id/ResultTitleTextView"
        android:layout_alignStart="@+id/ResultTitleTextView" />

    <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_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="onClickShare" />
>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

经过大量的搜索,我发现了我必须改变的东西。

getActivity()在所有5个地方都被 Results.this 取代。