recyclerview的setAdapter()导致崩溃,说相对布局中不存在循环依赖

时间:2018-04-05 08:20:12

标签: java android android-recyclerview

我正在使用一个简单的活动向Recyclerview显示一些数据,但是当我填充适配器并调用setAdapter时,我的应用程序崩溃时出现此错误,这只发生在Marshmallow和apis下面。它在Nougat和Oreo工作正常。

FATAL EXCEPTION: main
Process: com.example.aamishkhan.tablayoutandbottomnavigationviewsample, PID: 11001
java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1724)
at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:382)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:389)
at android.view.View.measure(View.java:18788)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:18788)
at android.support.v7.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:8994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1585)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

是的我知道相对布局中的循环引用。但是我在xml文件中看不到任何循环引用。它只包含一个Recyclerview。

我的主要java类

package com.example.aamishkhan.tablayoutandbottomnavigationviewsample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class RecyclerViewActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private StocksRvAdapter adapter;
private ArrayList<StocksBean> stocksIndicesBeanList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycler_view);

    recyclerView = findViewById(R.id.recycler);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
    recyclerView.setHasFixedSize(true);

    for (int i = 0; i < 10; i++) {
        StocksBean stocksBean = new StocksBean("First", "REG", "1.0", "2.0", "3.0",
                "50", "50.0", "60", "abc", "false",
                "100", "110", "0950", "5000", "20",
                "0", "50.0", "80.0", "open", "10000",
                "4000", "12052017", "80");

        stocksIndicesBeanList.add(stocksBean);
    }

    adapter = new StocksRvAdapter(this, stocksIndicesBeanList, "indexes");
    recyclerView.setAdapter(adapter);
}
}

我的自定义适配器布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#07223a">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="5dp"
    android:background="?attr/selectableItemBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <RelativeLayout
        android:id="@+id/mainrl"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/separatorend"
        android:layout_alignEnd="@+id/separatorend">

        <TextView
            android:id="@+id/symbolname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginRight="5dp"
            android:text=""
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/symbolfullname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/symbolname"
            android:text=""
            android:textColor="#bec191"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/netchange"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/symbolfullname"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true"
            android:text=""
            android:textColor="#49d4b5"
            android:textSize="20sp"
            android:textStyle="bold" />

        <RelativeLayout
            android:id="@+id/netpercrl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="#19262f">

            <TextView
                android:id="@+id/netchangepercent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:gravity="center"
                android:text=""
                android:textColor="#49d4b5"
                android:textSize="15sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/netchangepercent" />

        </RelativeLayout>

    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:id="@+id/mainseparator"
        android:layout_below="@+id/mainrl"
        android:background="#274a60" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/linearLayout"
        android:layout_below="@+id/mainrl"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="#19262f"
            android:orientation="horizontal"
            android:weightSum="6">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1">

                <TextView
                    android:id="@+id/hi"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:text="Hi"
                    android:textStyle="bold"
                    android:textColor="@android:color/holo_green_dark"
                    android:textSize="15sp" />

            </RelativeLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#274a60" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/low"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Low"
                    android:textStyle="bold"
                    android:textColor="@android:color/holo_red_dark"
                    android:textSize="15sp" />

            </RelativeLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#274a60" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/ldcp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="LDCP"
                    android:textStyle="bold"
                    android:textSize="15sp"
                    android:textColor="@android:color/white"/>

            </RelativeLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#274a60" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/lasttradeprice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="LTP"
                    android:textStyle="bold"
                    android:textSize="15sp"
                    android:textColor="@android:color/white"/>

            </RelativeLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#274a60" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/volume"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Vol"
                    android:textStyle="bold"
                    android:textSize="15sp"
                    android:textColor="@android:color/white"/>

            </RelativeLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#274a60" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:background="#19262f"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/totaltrades"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Trades"
                    android:textStyle="bold"
                    android:textSize="15sp"
                    android:textColor="@android:color/white"/>

            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#d6e8e4"
            android:orientation="horizontal"
            android:weightSum="6">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/hivalue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textColor="@android:color/holo_green_dark"
                    android:textSize="15sp" />

            </RelativeLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#274a60" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:background="#9daeaa"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/lowvalue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text=""
                    android:textColor="@android:color/holo_red_dark"
                    android:textSize="15sp" />

            </RelativeLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#274a60" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/ldcpvalue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textColor="@android:color/black"
                    android:textSize="15sp" />

            </RelativeLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#274a60" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/lasttradepricevalue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textColor="@android:color/black"
                    android:textSize="15sp" />

            </RelativeLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#274a60" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/volumevalue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textColor="@android:color/black"
                    android:textSize="15sp" />

            </RelativeLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#274a60" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/totaltradesvalue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textColor="@android:color/black"
                    android:textSize="15sp" />

            </RelativeLayout>

        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_width="2dp"
        android:layout_height="wrap_content"
        android:id="@+id/separatorend"
        android:background="#ebebeb"
        android:layout_toRightOf="@+id/mainrl"
        android:layout_alignBottom="@+id/mainseparator"
        android:layout_toStartOf="@+id/imagesrl"
        android:layout_toLeftOf="@+id/imagesrl"/>

    <RelativeLayout
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignBottom="@+id/mainseparator"
        android:id="@+id/imagesrl"
        android:layout_marginLeft="5dp">

        <!--<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:orientation="vertical"
            android:weightSum="1">-->

            <ImageView
                android:id="@+id/add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@mipmap/ic_launcher" />

            <!--<ImageView
                android:id="@+id/details"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_weight="0.5"
                android:src="@drawable/analytics_sign" />-->

        <!--</LinearLayout>-->

    </RelativeLayout>

</RelativeLayout>

我的适配器类

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

Context context;
ArrayList<StocksBean> allItems = new ArrayList<>();
ArrayList<StocksBean> result = new ArrayList<>();
LayoutInflater mInflater = null;
ArrayList<Integer> sectionForPosition = new ArrayList<Integer>();
ArrayList<Integer> positionForSection = new ArrayList<Integer>();
HashMap<String, Integer> sectionsMap = new HashMap<String, Integer>();
ArrayList<String> sectionsList = new ArrayList<String>();
String searchType = "Scrip";
private String sector = "";
private String market = "";

public StocksRvAdapter(Context context, ArrayList<StocksBean> listItems, String searchType) {
    this.context = context;
    this.searchType = searchType;
    //mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (listItems != null) {
        this.allItems = listItems;
        this.result = listItems;
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    // create a new view
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View v = inflater.inflate(R.layout.stocks_item_rv, parent, false);
    // set the view's size, margins, paddings and layout parameters
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

String netChange;
String netChangePercentage;

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

    if (allItems.size() > 0) {

        StocksBean rowItem = allItems.get(position);

        holder.symbolName.setText(rowItem.getSymbolCode());
        holder.symbolFullName.setText(rowItem.getSymbolCode());
        holder.netChangetv.setText(rowItem.getNetChange());
        holder.netChangetvPercentage.setText(rowItem.getNetChangePercent());
        holder.hiValue.setText(rowItem.getHighPrice());
        holder.lowValue.setText(rowItem.getLowPrice());
        holder.ldcpValue.setText(rowItem.getLastDayClosePrice());
        holder.ltpValue.setText(rowItem.getLastTradePrice());
        holder.volumeValue.setText(rowItem.getTotalTradeVolume());
        holder.totalTradesValue.setText(rowItem.getLastTradeVolume());
    }
}

@Override
public int getItemCount() {
    return allItems.size();
}

public String getSector() {
    return sector;
}

public void setSector(String sector) {
    this.sector = sector;
}

public String getMarket() {

    return market;
}

public void setMarket(String market) {
    this.market = market;
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private TextView symbolName;
    private TextView symbolFullName;
    private TextView netChangetv;
    private TextView netChangetvPercentage;
    private ImageView image;
    private TextView hiValue;
    private TextView lowValue;
    private TextView ldcpValue;
    private TextView ltpValue;
    private TextView volumeValue;
    private TextView totalTradesValue;
    private ImageView addImage;
    private ImageView detailsImage;

    private ViewHolder(View view) {
        super(view);
        symbolName = (TextView) view.findViewById(R.id.symbolname);
        symbolFullName = (TextView) view.findViewById(R.id.symbolfullname);
        netChangetv = (TextView) view.findViewById(R.id.netchange);
        netChangetvPercentage = (TextView) view.findViewById(R.id.netchangepercent);
        image = (ImageView) view.findViewById(R.id.image);
        hiValue = (TextView) view.findViewById(R.id.hivalue);
        lowValue = (TextView) view.findViewById(R.id.lowvalue);
        ldcpValue = (TextView) view.findViewById(R.id.ldcpvalue);
        ltpValue = (TextView) view.findViewById(R.id.lasttradepricevalue);
        volumeValue = (TextView) view.findViewById(R.id.volumevalue);
        totalTradesValue = (TextView) view.findViewById(R.id.totaltradesvalue);
        addImage = (ImageView) view.findViewById(R.id.add);
        //detailsImage = view.findViewById(R.id.details);
    }
}
}

我现在已经坚持了2天,仍然无法找到解决方案。

1 个答案:

答案 0 :(得分:0)

解决了它。这行是我的适配器xml android中的问题:layout_toRightOf =“@ + id / mainrl”

<View
android:layout_width="2dp"
android:layout_height="wrap_content"
android:id="@+id/separatorend"
android:background="#ebebeb"
android:layout_toRightOf="@+id/mainrl"
android:layout_alignBottom="@+id/mainseparator"
android:layout_toStartOf="@+id/imagesrl"
android:layout_toLeftOf="@+id/imagesrl"/>

不知何故,它没有在Nougat和上面的API中被检测到,但是在Marshmallow上面,它被系统检测到,因此导致崩溃。删除此行解决了崩溃问题。