ボタンをクリックして Facebook ウォールにサンプル メッセージを投稿しようとしています。ログイン ページが表示され、エラーが表示されると、以下のコードがクラッシュします。
05-07 20:22:39.843: E/AndroidRuntime(810): java.lang.NullPointerException 05-07 20:22:39.843: E/AndroidRuntime(810): 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();
}
}
};
}