java - RecyclerView.Adapter中的getItemCount() 返回数组的size是出现异常?
PHPz
PHPz 2017-06-05 11:12:35
[Android讨论组]

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.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.相关代码:
CityAdapter:


public class CityAdapter extends RecyclerView.Adapter {

    private List 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 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();
    }

}
                                     

AddActivity:

private List dataList = new ArrayList<>();
private RecyclerView mRecyclerView;
private List 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
PHPz
PHPz

学习是最好的投资!

全部回复(1)
我想大声告诉你

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

cityList = new ArrayList();
CityAdapter adapter=new CityAdapter(cityList);
//之后给cityList赋值是不再直接使用 cityList = DataSupport()...
cityList.clear();
cityList.addAll(DataSupport());
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号