以下のコードは、AndroidでfacebookSDK3.0を使用してステータスの更新を投稿するためのものです。私がUiLifeCycleHelper
クラスを使用している場合、それはうまく機能しています。UiLifeCycleHelper
でもクラスを使わずにFacebookにステータスを投稿したいです。以下のコードを試しましたが、logcatでエラーがスローされ、アプリケーションが強制的に閉じられます。
public class SampleFBPostActivity extends Activity implements OnClickListener {
private EditText mPostEditText;
private Button mDoneButton;
private Session mSession;
private PendingAction pendingAction = PendingAction.NONE;
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private enum PendingAction {
NONE,
POST_STATUS_UPDATE
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fb_post);
mPostEditText = (EditText) findViewById(R.id.post_text);
mDoneButton = (Button) findViewById(R.id.done_button);
mDoneButton.setOnClickListener(this);
mSession = Session.getActiveSession();
if (mSession == null) {
mSession = new Session(this);
Session.setActiveSession(mSession);
}
updateUI();
handlePendingAction();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.done_button:
onClickPostStatusUpdate();
break;
}
}
private interface GraphObjectWithId extends GraphObject {
String getId();
}
private void showPublishResult(String message, GraphObject result, FacebookRequestError error) {
String title = null;
String alertMessage = null;
if (error == null) {
title = getString(R.string.success);
String id = result.cast(GraphObjectWithId.class).getId();
alertMessage = getString(R.string.successfully_posted_post, message, id);
} else {
title = getString(R.string.error);
alertMessage = error.getErrorMessage();
}
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(alertMessage)
.setPositiveButton(R.string.ok, null)
.show();
}
private void onClickPostStatusUpdate() {
performPublish(PendingAction.POST_STATUS_UPDATE);
}
private void postStatusUpdate(final String message) {
if (mSession != null && hasPublishPermission()) {
Request request = Request
.newStatusUpdateRequest(Session.getActiveSession(), message, new Request.Callback() {
@Override
public void onCompleted(Response response) {
showPublishResult(message, response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
private boolean hasPublishPermission() {
Session session = Session.getActiveSession();
return session != null && session.getPermissions().contains("publish_actions");
}
private void performPublish(PendingAction action) {
Session session = Session.getActiveSession();
if (session != null) {
pendingAction = action;
if (hasPublishPermission()) {
handlePendingAction();
} else {
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, PERMISSIONS));
}
}
}
private void updateUI() {
Session session = Session.getActiveSession();
boolean enableButtons = (session != null && session.isOpened());
mDoneButton.setEnabled(enableButtons);
}
private void handlePendingAction() {
PendingAction previouslyPendingAction = pendingAction;
pendingAction = PendingAction.NONE;
switch (previouslyPendingAction) {
case POST_STATUS_UPDATE:
postStatusUpdate(mPostEditText.getText().toString());
break;
}
}
}
以下はlogcatのエラーです:
02-09 14:00:36.126: E/AndroidRuntime(2396): java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has a pending request.
02-09 14:00:36.126: E/AndroidRuntime(2396): at com.facebook.Session.requestNewPermissions(Session.java:968)
02-09 14:00:36.126: E/AndroidRuntime(2396): at com.facebook.Session.requestNewPublishPermissions(Session.java:501)
02-09 14:00:36.126: E/AndroidRuntime(2396): at com.facebook.samples.sessionlogin.SampleFBPostActivity.performPublish(SampleFBPostActivity.java:113)
02-09 14:00:36.126: E/AndroidRuntime(2396): at com.facebook.samples.sessionlogin.SampleFBPostActivity.onClickPostStatusUpdate(SampleFBPostActivity.java:83)
02-09 14:00:36.126: E/AndroidRuntime(2396): at com.facebook.samples.sessionlogin.SampleFBPostActivity.onClick(SampleFBPostActivity.java:54)
02-09 14:00:36.126: E/AndroidRuntime(2396): at android.view.View.performClick(View.java:3511)
02-09 14:00:36.126: E/AndroidRuntime(2396): at android.view.View$PerformClick.run(View.java:14105)
02-09 14:00:36.126: E/AndroidRuntime(2396): at android.os.Handler.handleCallback(Handler.java:605)
02-09 14:00:36.126: E/AndroidRuntime(2396): at android.os.Handler.dispatchMessage(Handler.java:92)
02-09 14:00:36.126: E/AndroidRuntime(2396): at android.os.Looper.loop(Looper.java:137)
02-09 14:00:36.126: E/AndroidRuntime(2396): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-09 14:00:36.126: E/AndroidRuntime(2396): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 14:00:36.126: E/AndroidRuntime(2396): at java.lang.reflect.Method.invoke(Method.java:511)
02-09 14:00:36.126: E/AndroidRuntime(2396): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-09 14:00:36.126: E/AndroidRuntime(2396): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-09 14:00:36.126: E/AndroidRuntime(2396): at dalvik.system.NativeStart.main(Native Method)
UiLifeCycleHelper
ソリューションでクラスを使用しないことで私を助けてください。前もって感謝します