在Facebook图表请求回调中,getActivity为null [英] getActivity is null in Facebook graph request callback

查看:159
本文介绍了在Facebook图表请求回调中,getActivity为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Facebook图表请求回调的问题。在某些设备中,getActivity为null。此代码正在执行一个片段,导致此问题?请帮助我修复它。

  GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback()
{

@Override
public void onCompleted(JSONObject object ,GraphResponse响应)
{
// here getActivity()为null

}
});


解决方案

在调用onAttach时保留活动引用(在您的片段中),并在需要的地方使用活动引用:

  @Override 
public void onAttach(Context context){
super.onAttach(context);
mContext = context;
}

当您在片段后完成的另一个线程中调用getActivity()已被删除。就像在HTTP请求完成时(例如onResponse)中调用getActivity()(例如Toast)的情况一样。


I have an issue with Facebook graph request callback. in some devices getActivity is null.This code is executing in a fragment is that causing this issue ?.Please help me to fix it .

GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                                         new GraphRequest.GraphJSONObjectCallback() 
 {

     @Override
     public void onCompleted(JSONObject object, GraphResponse response)
      {
          //here getActivity() is null

      }
});

解决方案

keep activity reference when onAttach is called (in your fragment) and use the activity reference wherever needed:

@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

This happened when you call getActivity() in another thread that finished after the fragment has been removed. Like in case of calling getActivity() (ex. for a Toast) when an HTTP request finished (in onResponse for example).

这篇关于在Facebook图表请求回调中,getActivity为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆