Android fragments 在片段的onCreate方法中,改装线程下的适配器为null
这是我的代码:Android fragments 在片段的onCreate方法中,改装线程下的适配器为null,android-fragments,adapter,retrofit2,Android Fragments,Adapter,Retrofit2,这是我的代码: public class TopicsFragment extends Fragment { TopicList topicList; List<Map<String, Object>> topics; TopicsAdapter adapter; FragmentActivity listener; @Override public void onAttach(Context context) {
public class TopicsFragment extends Fragment {
TopicList topicList;
List<Map<String, Object>> topics;
TopicsAdapter adapter;
FragmentActivity listener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity) {
this.listener = (FragmentActivity) context;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RubyChinaService service = RubyChinaApi.getInstance().getRubyChinaService();
//Create a call instance for looking up Retrofit topics.
Call<TopicList> call = service.getTopics();
call.enqueue(new Callback<TopicList>() {
@Override
public void onResponse(Call<TopicList> call, Response<TopicList> response) {
topicList = response.body();
topics = DataUtil.ObjectToMapList(topicList);
adapter = new TopicsAdapter(listener, topics, R.layout.item_topic,
new String[]{"authorIcon", "topicTitle", "topicAuthor", "repliesCount"},
new int[]{R.id.authorIcon, R.id.topicTitle, R.id.topicAuthor, R.id.repliesCount});
}
@Override
public void onFailure(Call<TopicList> call, Throwable t) {
t.printStackTrace();
}
});
}
}
公共类TopicsFragment扩展了片段{
主题列表主题列表;
列出主题;
Topics适配器;
碎片活动监听器;
@凌驾
公共void-onAttach(上下文){
super.onAttach(上下文);
if(活动的上下文实例){
this.listener=(FragmentActivity)上下文;
}
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
RubyChinaService service=RubyChinaApi.getInstance().getRubyChinaService();
//创建用于查找改装主题的调用实例。
Call=service.getTopics();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
topicList=response.body();
topics=DataUtil.ObjectToMapList(topicList);
适配器=新主题适配器(侦听器、主题、R.layout.item_主题、,
新字符串[]{“authorIcon”、“topicTitle”、“topicAuthor”、“repliesCount”},
新的int[]{R.id.authorIcon,R.id.topicTitle,R.id.topicAuthor,R.id.replesscount});
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
t、 printStackTrace();
}
});
}
}
当我在Android Studio中使用调试功能并创建断点时,我得到了一个空适配器。
为什么?
如何解决问题或组织我的代码