【发布时间】:2021-05-18 13:56:12
【问题描述】:
我正在尝试使用 Retrofit 从 API 获取数据并在微调器中显示,但它抛出错误“java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator() ' 在空对象引用上"
下面是我的 MainActivity 类
spinnerCode = findViewById(R.id.spinner_code);
UserNumber userNumber = ApiClient.getRetrofit().create(UserNumber.class);
Call<List<CountryCodes>> call = userNumber.countryCode();
call.enqueue(new Callback<List<CountryCodes>>() {
List<CountryCodes> data=new ArrayList<>();
@Override
public void onResponse(Call<List<CountryCodes>> listCall, Response<List<CountryCodes>> response) {
data = response.body();
for(int i=0;i<data.size();i++){
String item = data.get(i).getCountry_Name();
List<String> listSpinner = new ArrayList<String>();
listSpinner.add(item);
ArrayAdapter adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,listSpinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCode.setAdapter(adapter);
}
}
@Override
public void onFailure(Call<List<CountryCodes>> call, Throwable t) {
}
});
这是我的模型响应类
public class CountryCodes {
public String Country_Name;
public String CountryCode;
public String getCountry_Name() {
return Country_Name;
}
public void setCountry_Name(String country_Name) {
Country_Name = country_Name;
}
public String getCountryCode() {
return CountryCode;
}
public void setCountryCode(String countryCode) {
CountryCode = countryCode;
}
}
这是我的接口类
@POST("List_All_Available_Countries")
Call<List<CountryCodes>> countryCode();
【问题讨论】:
-
你在哪一行出错了??
-
在Main Activity Class这个"String item = data.get(i).getCountry_Name();"
-
请帮助我@D_K
-
请帮我解决这个错误
标签: java android android-studio android-spinner