Maison > Questions et réponses > le corps du texte
1. Description du problème : la taille du tableau renvoyé par getItemCount() dans RecyclerView.Adapter est anormale. Le réseau utilise okhttp et le framework de base de données est Litepal. La fonction consiste à obtenir des données JSON du réseau et à les stocker dans la base de données. puis à partir de La base de données est lue et affichée sur Addcity.
2. Message d'erreur principal :
FATAL EXCEPTION: main
Process: com.example.huang.easyweather, PID: 8208
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.huang.easyweather.CityAdapter.getItemCount(CityAdapter.java:68)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3493)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3310)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17637)
at android.view.ViewGroup.layout(ViewGroup.java:5575)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
3.Code associé :
CityAdapter :
public class CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder> {
private List<City> mCityList;
public class ViewHolder extends RecyclerView.ViewHolder{
TextView listItemCityView;
public ViewHolder(View itemView){
super(itemView);
listItemCityView=(TextView)itemView.findViewById(R.id.tv_item_city);
}
}
public CityAdapter(List<City> cityList){
mCityList=cityList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.city_list_item,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
City city=mCityList.get(position);
holder.listItemCityView.setText(city.getCityZh());
}
@Override
public int getItemCount() {
// if(null==mCityList) return 0; //如果有这条语句是不会报错的,但是RecyclerView获取不到数据
return mCityList.size();
}
}
Ajouter une activité :
private List<String> dataList = new ArrayList<>();
private RecyclerView mRecyclerView;
private List<City> cityList; //城市列表
mRecyclerView=(RecyclerView)findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);//设置布局管理器
CityAdapter adapter=new CityAdapter(cityList);
mRecyclerView.setAdapter(adapter);//设置适配器
//处理获取到的JSON数据并把它们存储在数据库中
public static boolean handleCityResponse(String response){
try {
JSONArray allcities=new JSONArray(response);
for (int i=0;i<allcities.length();i++){
JSONObject cityObject=allcities.getJSONObject(i);
City city=new City();
city.setCityId(cityObject.getString("id")); //城市id
city.setCityZh(cityObject.getString("cityZh"));//城市中文名
city.setLeaderZh(cityObject.getString("leaderZh"));//所属市级中文名
city.setProvinceZh(cityObject.getString("provinceZh"));//所属省级中文名
city.setCountryZh(cityObject.getString("countryZh"));//所属国家中文名
//储存在数据库中
city.save();
// Log.d("AddCity","获取的JSON数据存储到数据库成功");
}
return true;
}catch (JSONException e){
e.printStackTrace();
}
return false;
}
public void queryCities(String mSearchCity){
cityList= DataSupport
.select("cityZh")
.where("cityZh = ?",mSearchCity)
.find(City.class);
Log.d("AddCity","1");
for(City city:cityList){
dataList.add(city.getCityZh());
Log.d("AddCity",city.getCityZh());
}
}
public void queryFromServer(String cityAddressJson) {
NetworkUtils.sendOkHttpRequest(cityAddressJson, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("AddCity","HTTP连接失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String returnCityName=response.body().string(); //设置希望返回的字符串
boolean result=false;
result=handleCityResponse(returnCityName);//处理返回的JSON数据
if(result){ //如果成功处理,就开始从数据库查询
runOnUiThread(new Runnable() {
@Override
public void run() {
queryCities("北京");
}
});
}
}
});
}
我想大声告诉你2017-06-05 11:14:35
Lorsque vous l'êtes CityAdapter adapter=new CityAdapter(cityList);
, cityList n'a pas encore été initialisée. La bonne approche est
cityList = new ArrayList();
CityAdapter adapter=new CityAdapter(cityList);
//之后给cityList赋值是不再直接使用 cityList = DataSupport()...
cityList.clear();
cityList.addAll(DataSupport());