Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改装:片段中的listview_Android_Retrofit2 - Fatal编程技术网

Android 改装:片段中的listview

Android 改装:片段中的listview,android,retrofit2,Android,Retrofit2,我使用的标签有三个片段。 我正试图通过改型2在片段(TwoFragment)中加载一些数据。 但是我越来越 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)” public class TwoFragment extends Fragment{ @Override public View onCrea

我使用的标签有三个片段。 我正试图通过改型2在片段(TwoFragment)中加载一些数据。 但是我越来越 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)”

public class TwoFragment extends Fragment{

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

        View rootView =   inflater.inflate(R.layout.fragment_two, container, false);
        final ListView listView=(ListView)rootView.findViewById(R.id.listviewupgradeTo);

        LoginService loginService = ServiceGenerator.createService(LoginService.class,"abc","123");
        Call<List<UpgradeInfo>> call=loginService.getListInfo(i);
        call.enqueue(new Callback<List<UpgradeInfo>>() {
            @Override
            public void onResponse(Call<List<UpgradeInfo>> call, Response<List<UpgradeInfo>> response) {
                List<UpgradeInfo> list=response.body();
                listView.setAdapter(new UpgrageInfoRecordListViewAdapter(getContext(),list));
            }

            @Override
            public void onFailure(Call<List<UpgradeInfo>> call, Throwable t) {

            }
        });
        return rootView;
    }


}
公共类TwoFragment扩展了片段{
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u-two,容器,false);
最终ListView ListView=(ListView)rootView.findViewById(R.id.listviewupgradeTo);
LoginService LoginService=ServiceGenerator.createService(LoginService.class,“abc”,“123”);
Call Call=loginService.getListInfo(i);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
List=response.body();
setAdapter(新的UpgrageInfoRecordListViewAdapter(getContext(),list));
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
返回rootView;
}
}

从连接接收模型时,应始终执行空检查。您还应该检查响应是否有效。最后,列表不是响应模型itslef的主体。它包含在身体里。 像这样:

if(response.issusccessful()&&response.body()!=null){//首先进行检查
List List=response.body().getUpgradeInfo();//在这里,您应该从模型中获取列表
如果(list!=null){//进行null检查
setAdapter(新的
UpgrageInfoRecordListViewAdapter(getContext(),list));
}
}

让我知道结果如何。

首先,您不必设置适配器并创建响应列表。。只需在类中创建一个私有变量,当您得到响应时,通知适配器有关更改。其次,问题是什么?崩溃/未按预期工作?我试图使用retrofir2获取片段中的数据-然后发生了什么?您的问题是什么?问题是空指针异常,什么是getUpgradeInfo()?我应该在哪里写它?它是在响应模型中创建的方法。你可以给它取任何你想要的名字。这只是一个例子。关键是,您可以从响应的主体内部获取列表。列表不是主体本身。为什么不看看改装文档:。您可能还想查看未来工作室的教程。它们很好:
if (response.isSuccessful() && response.body() != null) { // make this first check
    List<UpgradeInfo> list=response.body().getUpgradeInfo(); // here, you should get the list from within the model
    if (list != null) { // make the null check
        listView.setAdapter(new 
UpgrageInfoRecordListViewAdapter(getContext(),list));
    }
}