我正在开发我的第一个Android应用程序,并尝试请求addtional publish_actions权限。在文档之后,我仅在需要时请求它们。
但是我收到了这个错误:
02-19 16:19:19.544: E/AndroidRuntime(12456): FATAL EXCEPTION: main
02-19 16:19:19.544: E/AndroidRuntime(12456): java.lang.IllegalStateException: Fragment MainFragment{40d66eb0} not attached to Activity
02-19 16:19:19.544: E/AndroidRuntime(12456): at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:889)
02-19 16:19:19.544: E/AndroidRuntime(12456): at com.facebook.Session$AuthorizationRequest$2.startActivityForResult(Session.java:1920)
02-19 16:19:19.544: E/AndroidRuntime(12456): at com.facebook.Session.tryLoginActivity(Session.java:1409)
02-19 16:19:19.544: E/AndroidRuntime(12456): at com.facebook.Session.authorize(Session.java:1159)
02-19 16:19:19.544: E/AndroidRuntime(12456): at com.facebook.Session.requestNewPermissions(Session.java:1264)
02-19 16:19:19.544: E/AndroidRuntime(12456): at com.facebook.Session.requestNewPublishPermissions(Session.java:595)
02-19 16:19:19.544: E/AndroidRuntime(12456): at com.cp1.ActivityRestaurantDetails$5$2.onClick(ActivityRestaurantDetails.java:332)
02-19 16:19:19.544: E/AndroidRuntime(12456): at android.view.View.performClick(View.java:3538)
我一直在这里读到有关碎片但我似乎无法解决这个问题。这是调用我的片段的代码
ActivityRestaurantDetails.java
btn_facebook.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Session session = Session.getActiveSession();
if (session != null) {
// Check for publish permissions
List<String> permissions = session.getPermissions();
String test = "nothing";
if (!isSubsetOf(SplashScreen.PERMISSIONS, permissions)) {
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(mainFragment, Arrays.asList("publish_actions"));
session.requestNewPublishPermissions(newPermissionsRequest);
}
}
Intent intent = new Intent(getParent(), ActivitySharing.class);
intent.putExtra("parent_class", "ActivityRestaurantDetails");
intent.putExtra("share_type","facebook");
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("ActivitySharing", intent);
}
});
这是我的片段代码...... MainFragment.java
public class MainFragment extends Fragment {
private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
public void onSessionStateChange(Session session, SessionState state, Exception exception)
{
if (state.isOpened())
{
Log.i("facebook", "Logged in...");
Request.newMeRequest(session, new Request.GraphUserCallback()
{
@Override
public void onCompleted(GraphUser user, Response response)
{
if(SplashScreen.fb_login==false){
if(user!=null)
{
SplashScreen.fb_login=true;
SplashScreen.first_name=user.getFirstName()+"";
SplashScreen.last_name=user.getLastName()+"";
SplashScreen.fb_id=user.getId()+"";
SplashScreen.birthday=user.getBirthday()+"";
SplashScreen.fb_page=user.getLink()+"";
SplashScreen.email=user.asMap().get("email") + "";
SplashScreen.username=user.asMap().get("email") + "";
SplashScreen.gender=user.asMap().get("gender") + "";
SplashScreen.timezone=user.asMap().get("timezone") + "";
if(SplashScreen.fb_status.equals("login")){
SplashScreen.AL.Login();
}else if(SplashScreen.fb_status.equals("logout")){
}else if(SplashScreen.fb_status.equals("register")){
SplashScreen.AR.getCode();
}
}
else
{
String test=("its null");
test=(response.getError().getErrorMessage());
}
}
}
}).executeAsync();
}
else if (state.isClosed())
{
Log.i("facebook", "Logged out...");
}
}
@Override
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
知道如何解决这个问题吗?我在ActivityRestaurantDetails中设置了onActivityResult方法。