RecyclerView.Adapter中的getItemCount() 返回数组的size是出现异常?
澳门新葡京官网
澳门新葡京官网
当前位置 : 澳门新葡京官网 > 网上赌博新址

RecyclerView.Adapter中的getItemCount() 返回数组的size是出现异常?

1.问题描述:RecyclerView.Adapter中的getItemCount 返回数组的size是出现异常.网络用的是okhttp,数据库框架是litepal,功能是从网络上获取JSON数据并把它们存入数据库然后再从数据库读取,在Addcity上显示出来。

2.主要报错信息:

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.getItemCountCityAdapter.java:68
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1RecyclerView.java:3493
at android.support.v7.widget.RecyclerView.dispatchLayoutRecyclerView.java:3310
at android.support.v7.widget.RecyclerView.onLayoutRecyclerView.java:3844
at android.view.View.layoutView.java:17637
at android.view.ViewGroup.layoutViewGroup.java:5575
at android.widget.LinearLayout.setChildFrameLinearLayout.java:1741
at android.widget.LinearLayout.layoutVerticalLinearLayout.java:1585
at android.widget.LinearLayout.onLayoutLinearLayout.java:1494
at android.view.View.layoutView.java:17637
at android.view.ViewGroup.layoutViewGroup.java:5575
at android.widget.FrameLayout.layoutChildrenFrameLayout.java:323
at android.widget.FrameLayout.onLayoutFrameLayout.java:261
at android.view.View.layoutView.java:17637
at android.view.ViewGroup.layoutViewGroup.java:5575
at android.support.v7.widget.ActionBarOverlayLayout.onLayoutActionBarOverlayLayout.java:437
at android.view.View.layoutView.java:17637
at android.view.ViewGroup.layoutViewGroup.java:5575
at android.widget.FrameLayout.layoutChildrenFrameLayout.java:323
at android.widget.FrameLayout.onLayoutFrameLayout.java:261
at android.view.View.layoutView.java:17637
at android.view.ViewGroup.layoutViewGroup.java:5575
at android.widget.LinearLayout.setChildFrameLinearLayout.java:1741

3.相关代码:
CityAdapter:


public class CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder> {

    private List<City> mCityList;

    public class ViewHolder extends RecyclerView.ViewHolder{

        TextView listItemCityView;
        public ViewHolderView itemView{
            superitemView;
            listItemCityView=TextViewitemView.findViewByIdR.id.tv_item_city;
        }

    }
    public CityAdapterList<City> cityList{
        mCityList=cityList;
    }
    @Override
    public ViewHolder onCreateViewHolderViewGroup viewGroup, int viewType {
    
        View view=LayoutInflater.fromviewGroup.getContext.inflateR.layout.city_list_item,viewGroup,false;
        return new ViewHolderview;
    }

    @Override
    public void onBindViewHolderViewHolder holder, int position {
        City city=mCityList.getposition;
        holder.listItemCityView.setTextcity.getCityZh;
    }

    @Override
    public int getItemCount {
       // ifnull==mCityList return 0;  //如果有这条语句是不会报错的,但是RecyclerView获取不到数据
        return mCityList.size;
    }

}
                                     

AddActivity:

private List<String> dataList = new ArrayList<>;
private RecyclerView mRecyclerView;
private List<City> cityList; //城市列表

 mRecyclerView=RecyclerViewfindViewByIdR.id.recycler_view;
        LinearLayoutManager layoutManager=new LinearLayoutManagerthis;
        mRecyclerView.setLayoutManagerlayoutManager;//设置布局管理器
        CityAdapter adapter=new CityAdaptercityList; 
        mRecyclerView.setAdapteradapter;//设置适配器
        
        
  //处理获取到的JSON数据并把它们存储在数据库中
    public static boolean handleCityResponseString response{
            try {
                JSONArray allcities=new JSONArrayresponse;
                for int i=0;i<allcities.length;i++{
                    JSONObject cityObject=allcities.getJSONObjecti;
                    City city=new City;
                    city.setCityIdcityObject.getString"id"; //城市id
                    city.setCityZhcityObject.getString"cityZh";//城市中文名
                    city.setLeaderZhcityObject.getString"leaderZh";//所属市级中文名
                    city.setProvinceZhcityObject.getString"provinceZh";//所属省级中文名
                    city.setCountryZhcityObject.getString"countryZh";//所属国家中文名
                    //储存在数据库中
                    city.save;
                   // Log.d"AddCity","获取的JSON数据存储到数据库成功";
                }
                return true;
            }catch JSONException e{
                e.printStackTrace;
            }
        return false;

    }

     public void queryCitiesString mSearchCity{
        cityList= DataSupport
                .select"cityZh"
                .where"cityZh = ?",mSearchCity
                .findCity.class;
        Log.d"AddCity","1";
            forCity city:cityList{
                dataList.addcity.getCityZh;
                Log.d"AddCity",city.getCityZh;
            }
    
    
    
        }
 public void queryFromServerString cityAddressJson {
        NetworkUtils.sendOkHttpRequestcityAddressJson, new Callback {

            @Override
            public void onFailureCall call, IOException e {
                Log.d"AddCity","HTTP连接失败";
            }

            @Override
            public void onResponseCall call, Response response throws IOException {
                String returnCityName=response.body.string; //设置希望返回的字符串
                boolean result=false;
                result=handleCityResponsereturnCityName;//处理返回的JSON数据

                ifresult{ //如果成功处理,就开始从数据库查询
                    runOnUiThreadnew Runnable {
                       @Override
                       public void run {
                        queryCities"北京";
                       }
                   };
                }
            }
        };
    }

你在CityAdapter adapter=new CityAdaptercityList;时,cityList还没有初始化,正确的做法是

cityList = new ArrayList;
CityAdapter adapter=new CityAdaptercityList;
//之后给cityList赋值是不再直接使用 cityList = DataSupport...
cityList.clear;
cityList.addAllDataSupport;

栏目列表

广告位

澳门新葡京官网