-1

我正在尝试通过单击按钮在 Facebook 墙上发布示例消息。当显示登录页面并显示错误时,以下代码崩溃:

05-07 20:22:39.843: E/AndroidRuntime(810): java.lang.NullPointerException 05-07 20:22:39.843: E/AndroidRuntime(810): at com.facebook.android.SessionStore.save(SessionStore. java:33) 05-07 20:22:39.843: E/AndroidRuntime(810): 在 com.test.fb.TestPost$FbLoginDialogListener.onComplete(TestPost.java:116) 05-07 20:22:39.843: E/ AndroidRuntime(810):在 com.facebook.android.Facebook$1.onComplete(Facebook.java:308) 05-07 20:22:39.843:E/AndroidRuntime(810):在 com.facebook.android.FbDialog$FbWebViewClient。 shouldOverrideUrlLoading(FbDialog.java:125)

任何帮助都会得到帮助。谢谢

package com.test.fb;

import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.app.Activity;
import android.app.ProgressDialog;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.Toast;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.BaseRequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.SessionStore;
import com.facebook.android.Facebook.DialogListener;


public class TestPost extends Activity{
    private TestPost _activity;
    private Facebook mFacebook;
    private ProgressDialog mProgress;

    private Handler mRunOnUi = new Handler();
    private static final String[] PERMISSIONS = new String[] {"publish_stream", "read_stream", "offline_access"};
    private static final String APP_ID = "331862293507865";

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

        setContentView(R.layout.main);

        //final EditText reviewEdit = (EditText) findViewById(R.id.revieew);
        //mFacebookCb                 = (CheckBox) findViewById(R.id.cb_facebook);

        mProgress   = new ProgressDialog(this);
        mFacebook   = new Facebook(APP_ID);
        SessionStore.restore(mFacebook, this);


        ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(mFacebook.isSessionValid()){

                String review = "Sample post";

                if (review.equals(""))
                    return;
                 postToFacebook(review);
                }
                else{
                    onFacebookClick();
                }
            }
        });
    }

    private void onFacebookClick() {
        if (mFacebook.isSessionValid()) {
            postToFacebook(null);
                } else {
            mFacebook.authorize(this, PERMISSIONS, -1, new FbLoginDialogListener());
        }
    }
    private void postToFacebook(String review) {    
        mProgress.setMessage("Posting ...");
        mProgress.show();

        AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook);

        Bundle params = new Bundle();

        params.putString("message", review);
        params.putString("name", "Dexter");
        params.putString("caption", "londatiga.net");
        params.putString("link", "http://www.londatiga.net");
        params.putString("description", "Dexter, seven years old dachshund who loves to catch cats, eat carrot and krupuk");
        params.putString("picture", "http://twitpic.com/show/thumb/6hqd44");

        mAsyncFbRunner.request("me/feed", params, "POST", new WallPostListener());
    }

    private final class WallPostListener extends BaseRequestListener {
        public void onComplete(final String response) {
            mRunOnUi.post(new Runnable() {

                public void run() {
                    mProgress.cancel();

                    Toast.makeText(TestPost.this, "Posted to Facebook", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    private final class FbLoginDialogListener implements DialogListener {
        public void onComplete(Bundle values) {
            SessionStore.save(mFacebook, _activity);
           getFbName();
        }

        public void onFacebookError(FacebookError error) {

        }

        public void onError(DialogError error) {

        }

        public void onCancel() {

        }
    }
    private void getFbName() {
        mProgress.setMessage("Finalizing ...");
        mProgress.show();

        new Thread() {
            @Override
            public void run() {
                String name = "";
                int what = 1;

                try {
                    String me = mFacebook.request("me");

                    JSONObject jsonObj = (JSONObject) new JSONTokener(me).nextValue();
                    name = jsonObj.getString("email");
                    what = 0;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                mFbHandler.sendMessage(mFbHandler.obtainMessage(what, name));
            }
        }.start();
    }

    private Handler mFbHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mProgress.dismiss();

            if (msg.what == 0) {
                String username = (String) msg.obj;
                username = (username.equals("")) ? "No Name" : username;

                SessionStore.saveName(username, _activity);

            } else {
                Toast.makeText(_activity, "Connected to Facebook", Toast.LENGTH_SHORT).show();
            }
        }
    };
}
4

1 回答 1

1

onComplete方法中,您尝试保存会话:

SessionStore.save(mFacebook, _activity);

我认为,问题在于您没有为“_activity”参数(在您发布的代码中)分配任何值,因此它为空。

无论如何,这个变量有什么意义?如果要从私有类中访问活动实例,那么您可以尝试:

SessionStore.save(mFacebook, TestPost.this);
于 2012-05-07T15:07:55.720 回答