简体   繁体   中英

facebook sdk 3.5 share dialog on android not working

I've been wandering in google and this site for two weeks now in order to find some answers for my problem, but i didnt get any. i've done a app in which the users can take pictures and, hopefuly, update them to facebook. now, i've manage to do a gallery and when the user press on image, it's open in a new class, called ViewImage. there i ass some option menu, such as settings and share. for now, i want that when the user press share, something will be post to his facebook profile, using share dialog (later i'll try open graph). here is the code for ViewImage, some of it is taken from https://gist.github.com/jacklt/6700201 :

public class ViewImage extends Activity {
    TextView text;
    ImageView imageview;
    private int position;
    private String[] filename;
    private String[] filepath;
    private UiLifecycleHelper uiHelper;
    private static final String TAG_LOG = ViewImage.class.getSimpleName();
    private FacebookDialog.Callback dialogCallback = new FacebookDialog.Callback() {
        @Override
        public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
            Log.d(TAG_LOG, String.format("Error: %s", error.toString()));
        }

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

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            Log.i(TAG_LOG, "Logged in...");
        } else if (state.isClosed()) {
            Log.i(TAG_LOG, "Logged out...");
        }
    }

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

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

        // Get the view from view_image.xml
        setContentView(R.layout.view_image); ;

        // Retrieve data from MainActivity on GridView item click
        Intent i = getIntent();

        // Get the position
        position = i.getExtras().getInt("position");

        // Get String arrays FilePathStrings
        filepath = i.getStringArrayExtra("filepath");

        // Get String arrays FileNameStrings
        filename = i.getStringArrayExtra("filename");

        // Locate the TextView in view_image.xml
        text = (TextView) findViewById(R.id.imagetext);

        // Load the text into the TextView followed by the position
        text.setText(filename[position]);

        // Locate the ImageView in view_image.xml
        imageview = (ImageView) findViewById(R.id.full_image_view);

        imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);

        // Decode the filepath with BitmapFactory followed by the position
        Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);

        bmp = GridViewAdapter.adjustImageOrientation(bmp,filepath[position]);

        // Set the decoded bitmap into ImageView
        imageview.setImageBitmap(bmp);

    }

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

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

    @Override
    public 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();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.view_image, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_home) {
            {
                Intent i = new Intent();
                i.setClass(this, Main.class);
                startActivity(i);
            }
            return true;
        } else if (item.getItemId() == R.id.action_settings) {
            {
                Intent i = new Intent("net.lirazarviv.Setting");
                startActivity(i);
            }
            return true;
        } else if (item.getItemId() == R.id.action_share) {
            {
                if (FacebookDialog.canPresentShareDialog(this, FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
                    FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
                            .setName("Titolo")
                            .setLink("http://developer.neosperience.com/android")
                            .setDescription("Hello from Neosperience Developer")
                            .setPicture("http://lh3.googleusercontent.com/-P4JBVTv_kSI/AAAAAAAAAAI/AAAAAAAAAAs/bZptjIhkWu4/s265-c-k-no/photo.jpg")
                            .build();
                    uiHelper.trackPendingDialogCall(shareDialog.present());

                }
                else {
                    Log.d(TAG_LOG, "Success!");
                }
            }

             return true;
        }  else return super.onOptionsItemSelected(item);
    }
}

when i press the share button, the app just crash.. some help with this will be appreciate! ps here is the logcat:

E/PlayerDriver(   83): PlayerDriver::handleTvOut state=[1]
10-03 15:21:45.893 E/PlayerDriver(   83): PlayerDriver::it is not a DRM file.So don't suspend TVOUT
10-03 15:21:53.440 E/AndroidRuntime( 1467): FATAL EXCEPTION: main
10-03 15:21:53.440 E/AndroidRuntime( 1467): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.lirazarviv.origame/net.lirazarviv.origame.ViewImage}: java.lang.NullPointerException: Argument 'applicationId' cannot be null
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at android.os.Looper.loop(Looper.java:123)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at android.app.ActivityThread.main(ActivityThread.java:3687)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at java.lang.reflect.Method.invokeNative(Native Method)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at java.lang.reflect.Method.invoke(Method.java:507)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at dalvik.system.NativeStart.main(Native Method)
10-03 15:21:53.440 E/AndroidRuntime( 1467): Caused by: java.lang.NullPointerException: Argument 'applicationId' cannot be null
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at com.facebook.internal.Validate.notNull(Validate.java:29)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at com.facebook.Session.<init>(Session.java:227)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at com.facebook.Session.<init>(Session.java:212)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at com.facebook.UiLifecycleHelper.onCreate(UiLifecycleHelper.java:87)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at net.lirazarviv.origame.ViewImage.onCreate(ViewImage.java:58)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-03 15:21:53.440 E/AndroidRuntime( 1467):     ... 11 more
10-03 15:21:53.748 E/        (  128): Dumpstate > /data/log/dumpstate_app_error

It's because you don't have the application id set in your manifest. Normally, if you're already using other SDK features (like Login), then you would have it in your manifest already, but if you're not, here's how to add it:

Within the application tag in your AndroidManifest.xml, add something like:

<application>
  ...
  <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />
</application>

Then, in your values/strings.xml, add

<string name="app_id">YOUR_FACEBOOK_APP_ID</string>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM