Android:AsyncTaskLoader从fragment启动时崩溃

时间:2013-02-08 13:44:55

标签: android android-fragments android-viewpager asynctaskloader

我有这个片段:

public class ResultFragment extends Fragment实现了LoaderCallbacks {

public static ResultFragment newInstance(Bundle args) {
ResultFragment fragment = new ResultFragment();
fragment.setArguments(args);
return fragment;
}

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


    getActivity().getSupportLoaderManager().initLoader(0, null, this);

}

@Override
public Loader<EstateSearch> onCreateLoader(int id, Bundle args) {

        return new RESTLoader(getActivity(), "http://etc");

}

@Override
public void onLoadFinished(Loader<EstateSearch> loader, EstateSearch es) {

}

@Override
public void onLoaderReset(Loader<EstateSearch> loader) {

}

}

AsyncTaskLoader看起来像这样:

公共类RESTLoader扩展了AsyncTaskLoader {

private String searchUrl; 

public RESTLoader(Context context, String searchUrl) {
    super(context);
    this.searchUrl = searchUrl; 


}

@Override
public EstateSearch loadInBackground() {


      EstateSearch es = null;       

      try {


    Network Stuff


    } catch (Exception e) {

    }

      return es; 
}

@Override
public void deliverResult(EstateSearch es) {


    super.deliverResult(es);
}

@Override
protected void onStartLoading() {
    forceLoad();
}

@Override
protected void onStopLoading() {

    cancelLoad();
}

@Override
protected void onReset() {
    super.onReset();        
    onStopLoading();     
}
}

该应用程序崩溃了一个神秘的(至少对我而言)错误:

http://i46.tinypic.com/260fw43.png

从放入一些Log.ds我知道甚至没有调用AsyncTaskLoader的构造函数。我已经尝试将加载器的init()移动到片段生命周期的后期部分。如果这很重要,那么片段就在ViewPager中。从Activity调用时,AsyncTaskLoader工作正常。

关于我做错的任何想法?

1 个答案:

答案 0 :(得分:0)

好吧,非常令人尴尬的错误:我已经在活动中有另一个Loader,该片段附加到也有ID 0: - /