LittleMonsterr
2017-03-26 10:11
采纳率: 100%
浏览 1.6k

android 两个页面都用recyclerview,一个正常,另一个报错

第一个页面(没问题)

     @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.no_details)
    LinearLayout noDetails;
    List<WalletDetailsBean.ResultBean.ListBean> transactionList = new ArrayList<WalletDetailsBean.ResultBean.ListBean>();
    private TransactionListAdapter adapter;
    private String token;
    private int page = 1;
        ....
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet_details);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        if(intent != null){
            token = intent.getStringExtra("token") != null ? intent.getStringExtra("token") : " ";
        }
        initTitle();
        initView();
        initData(token);
    }
        ....
        private void initView() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        refreshLayout.setProgressBackgroundColor(R.color.refresh_bg);
        initListView();
    }

    private void initListView() {
        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new TransactionListAdapter(this, transactionList);
        recyclerView.setAdapter(adapter);
        recyclerView.setBackgroundColor(Color.WHITE);
        // 第一次进入页面的时候显示加载进度条
        refreshLayout.setProgressViewOffset(true, 0,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24,
                        getResources().getDisplayMetrics()));
        refreshLayout.setRefreshing(true);
        //滚动监听
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                    refreshLayout.setRefreshing(true);
                    page++;
                    if (page <= totalPage) { //根据后台接口   判断当前页是否小于总页
                        L.e("pageNo:" + page + ",totalPage:" + totalPage);
                        queryTransactionDetails(MywalletDetailActivity.this,token,page);
                        showToast(getResources().getString(R.string.loading));
                    } else {
                        refreshLayout.setRefreshing(false);
                        showToast(getString(R.string.no_more_data));
                    }
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
    }

两个页面的xml文件都差不多:

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    ...
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:paddingTop="@dimen/common_10"
            android:background="#eaeaea"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

第二个页面:

  @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.no_schedule_ll)
    LinearLayout noScheduleLl;
    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_myschedule)
    SwipeRefreshLayout refreshLayout;
        ...
         @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_schedule);
        ButterKnife.bind(this);

        withRefreshView = setRefreshView();
        intent = getIntent();
        userid = intent.getStringExtra("userid");
        token = intent.getStringExtra("token");
        initView();
        loadFromNet(token,pageNo);
    }

    /**
     * 初始化控件
     */
    private void initView() {
        titleTv.setText(getResources().getString(R.string.my_schedule));
        needHelp.setVisibility(View.VISIBLE);
        needHelp.setText(getResources().getString(R.string.need_help));

        refreshLayout.setOnRefreshListener(this);

        refreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        refreshLayout.setProgressBackgroundColor(R.color.refresh_bg);
        refreshLayout.setRefreshing(true);
        /*
         *  第一次进入页面的时候显示加载进度条
         */
        refreshLayout.setProgressViewOffset(true, 0,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24,
                        getResources().getDisplayMetrics()));
        refreshLayout.setRefreshing(true);
        initListView();
    }

    private void initListView() {
        recyclerView.setItemAnimator(new DefaultItemAnimator());//设置item动画
        recyclerView.setHasFixedSize(true); //如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
        //创建默认的线性LayoutManage
        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setBackgroundColor(Color.WHITE);
        recyclerView.addItemDecoration(new MyDecoration(this, MyDecoration.VERTICAL_LIST));//自定义的分隔线

        adapter = new ScheduleAdapter(this, scheduleList);
        adapter.setOnItemClickListener(new ScheduleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivity(new Intent(MyScheduleActivity.this, ShowWebViewActivity.class)
                        .putExtra("title",getResources().getString(R.string.riding_details_title))
                        .putExtra("share",getResources().getString(R.string.riding_details_share))
                        .putExtra("url", Url.getMyScheduleDetailUrl(true) + "?token=" + token +"&id=" + rideId));
            }
        });
        adapter.setOnItemLongClickListener(new ScheduleAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        recyclerView.setAdapter(adapter);

        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                    refreshLayout.setRefreshing(true);
                    pageNo++;
                    if (pageNo <= totalPage) { //根据后台接口   判断当前页是否小于总页
                        L.e("pageNo:" + pageNo + "newsTotalPage:" + totalPage);
                        loadFromNet(token, pageNo);
                        showToast(getResources().getString(R.string.loading));
                    } else {
                        refreshLayout.setRefreshing(false);
                        showToast(getString(R.string.no_more_data));
                    }
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
    }

跳转到第一个页面正常显示列表,跳转到第二个页面的时候报错
图片说明

请问是怎么回事,看了一天了还是看不出所以然来。
谢谢~

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • LittleMonsterr 2017-03-27 01:37
    已采纳

    已解决,原因:没处理好屏幕适配。深坑,谨记教训!!

    图片说明

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 徐福记456 2017-03-26 12:37

    已经有错误提示:第二个页面,无法加载xml布局的第27行,android.support.v7.widget.RecyclerView。你去检查下

    评论
    解决 无用
    打赏 举报
  • ytmfdw 2017-03-26 15:58

    布局出错,看下导包v7包和design包版本是否一致

    评论
    解决 无用
    打赏 举报
  • csdn_zxw 2017-03-27 01:19

    人家说的是75行报错了,看看75行是什么

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题