IllegalStateException:添加github库后,片段未附加到Activity

时间:2016-10-20 07:07:04

标签: android android-fragments

在我的项目中,在视图寻呼机中显示了5个片段。我正在使用凌空将数据下载到片段中。所以我覆盖 setUserVisibleHint()来控制所选标签上的下载。一切都很好,但在添加了以下library然后得到 IllegalStateException

compile 'com.theartofdev.edmodo:android-image-cropper:2.3.+'

我删除了setUserVisibleHint(),一切都有效了但是有没有解决方案来拥有库和setUserVisibleHint()?

这是我的片段代码

public class SpecialPackages extends Fragment {

    private boolean isFragmentLoaded = false;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.special_list_fragment, container, false);

        //find views ...

        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser && !isFragmentLoaded){
            getSpecialPackage();
            isFragmentLoaded = true;
        }else{
            if (specialOfferList.size() > 0 && specialOfferList != null) {
                setListAdapter(specialOfferList);
            }
        }
    }

    private void getSpecialPackage(){
        StringRequest request = new StringRequest(Request.Method.POST, "http://example.com/test.php",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e(TAG, "Error: " + error.getMessage());
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(Keys.API_USERNAME, Tags.API_UN);
                params.put(Keys.API_PASSWORD, Tags.API_PASS);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(request, getString(R.string.get_special_offers));
    }
}

这是我的日志

java.lang.IllegalStateException: Fragment SpecialPackages{420dde78 id=0x7f0d01b4 android:switcher:2131558836:4} not attached to Activity

0 个答案:

没有答案