FirebaseRecyclerAdapter error: java.lang.Throwable: addInArr



我在Stackoverflow上发现了类似的问题,但没有人有工作答案,我无法弄清楚如何解决此问题。

我的 RecyclerView似乎运行良好,但加载缓慢,我会遇到此错误仅适用于4个项目的RecyclerView

我的错误日志(此错误20倍):

D/ViewGroup: addInArray been called, this = android.support.v7.widget.RecyclerView{...} ,child = android.widget.LinearLayout{...} ,call stack = java.lang.Throwable: addInArray
                                                                    at android.view.ViewGroup.addInArray(ViewGroup.java:3786)
                                                                    at android.view.ViewGroup.attachViewToParent(ViewGroup.java:4353)
                                                                    at android.support.v7.widget.RecyclerView.access$000(RecyclerView.java:155)
                                                                    at android.support.v7.widget.RecyclerView$5.attachViewToParent(RecyclerView.java:727)
                                                                    at android.support.v7.widget.ChildHelper.attachViewToParent(ChildHelper.java:239)
                                                                    at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:7385)
                                                                    at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:7362)
                                                                    at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:7350)
                                                                    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1459)
                                                                    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
                                                                    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
                                                                    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3379)
                                                                    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3194)
                                                                    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3632)
                                                                    at android.view.View.layout(View.java:15146)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4866)
                                                                    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160)
                                                                    at android.view.View.layout(View.java:15146)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4866)
                                                                    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
                                                                    at android.view.View.layout(View.java:15146)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4866)
                                                                    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160)
                                                                    at android.view.View.layout(View.java:15146)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4866)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
                                                                    at android.view.View.layout(View.java:15146)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4866)
                                                                    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
                                                                    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
                                                                    at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
                                                                    at android.view.View.layout(View.java:15146)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4866)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
                                                                    at android.view.View.layout(View.java:15146)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4866)
                                                                    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
                                                                    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
                                                                    at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
                                                                    at android.view.View.layout(View.java:15146)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4866)
                                                                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
                                                                    at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
                                                                    at android.view.View.layout(View.java:15146)
                                                                    at android.view.ViewGroup.layout(ViewGroup.java:4866)
                                                                    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
                                                                    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2048)
                                                                    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1211)
                                                                    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6282)
                                                                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
                                                                    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
                                                                    at android.view.Choreographer.doFrame(Choreographer.java:560)
                                                                    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
                                                                    at android.os.Handler.handleCallback(Handler.java:808)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                    at android.os.Looper.loop

这是我的代码:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
LinearLayoutManager manager = new LinearLayoutManager( this );
listRaces.setHasFixedSize(true);
FirebaseRecyclerAdapter mAdapter = new FirebaseRecyclerAdapter<Race, ViewHolder>( Race.class, R.layout.races_row, ViewHolder.class, ref.child(Constant.FIREBASE_RACES).orderByChild("runners/" + User.getId()).equalTo(true).limitToFirst(4)) {
    @Override
    protected void populateViewHolder(ViewHolder viewHolder, Race race, int position) {
        MyLog.log(String.valueOf(position));
        if(position<3) {
            viewHolder.setDistance(String.valueOf(race.distance) + " Km");
            viewHolder.setLevels(String.valueOf(race.level_min) + " - " + String.valueOf(race.level_max));
            viewHolder.setRunners(String.valueOf(race.signed_runners) + "/" + String.valueOf(race.max_runners));
            viewHolder.setTime(TimeHandler.timestampToDate(String.valueOf(race.date)));
        } else {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT );
            viewHolder.textListDistance.setLayoutParams(layoutParams);
            viewHolder.setDistance( "Ver todas..." );
            viewHolder.textListDistance.setTextSize(14f);
            viewHolder.textListDistance.setGravity(Gravity.CENTER);
            viewHolder.textListDistance.setTypeface(null, Typeface.ITALIC);
        }
    }
};
listRaces.setAdapter(mAdapter);
listRaces.setLayoutManager(manager);

这是我的ViewHolder类:

public static class ViewHolder extends RecyclerView.ViewHolder {
    private final TextView textListDistance;
    private final TextView textListLevels;
    private final TextView textListRunners;
    private final TextView textListTime;
    public ViewHolder(View itemView) {
        super(itemView);
        textListDistance = (TextView) itemView.findViewById(R.id.textListDistance);
        textListLevels = (TextView) itemView.findViewById(R.id.textListLevels);
        textListRunners = (TextView) itemView.findViewById(R.id.textListRunners);
        textListTime = (TextView) itemView.findViewById(R.id.textListTime);
    }
    public void setDistance(String distance) {
        textListDistance.setText(distance);
    }
    public void setLevels(String levels) {
        textListLevels.setText(levels);
    }
    public void setRunners(String runners) {
        textListRunners.setText(runners);
    }
    public void setTime(String time) {
        textListTime.setText(time);
    }
}

我真的很高兴为此提供任何帮助。谢谢。

编辑:

即使使用此简化代码,错误仍然存在:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
        LinearLayoutManager manager = new LinearLayoutManager( this );
        listRaces.setLayoutManager(manager);
        listRaces.setHasFixedSize(true);
        FirebaseRecyclerAdapter mAdapter = new FirebaseRecyclerAdapter<Race, ViewHolder>( Race.class, R.layout.races_row, ViewHolder.class, ref.child(Constant.FIREBASE_RACES).orderByChild("runners/" + User.getId()).equalTo(true).limitToFirst(4)) {
            @Override
            protected void populateViewHolder(ViewHolder viewHolder, Race race, int position) {
                    viewHolder.setDistance(String.valueOf(race.distance) + " Km");
                    viewHolder.setLevels(String.valueOf(race.level_min) + " - " + String.valueOf(race.level_max));
                    viewHolder.setRunners(String.valueOf(race.signed_runners) + "/" + String.valueOf(race.max_runners));
                    viewHolder.setTime(TimeHandler.timestampToDate(String.valueOf(race.date)));
            }
        };
        listRaces.setAdapter(mAdapter);

在这里我的races_row布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:background="@drawable/list_races_public"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="14dp">
    <TextView
        android:id="@+id/show_all"
        android:visibility="gone"
        android:gravity="center"
        android:text="Ver todas..."
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:textStyle="italic" />
    <TextView android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:text="5 Km"
        android:textStyle="bold"
        android:textSize="18dp"
        android:id="@+id/textListDistance"
        android:layout_marginRight="10dp"
        android:gravity="right" />
    <TextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:id="@+id/textListLevels"
        android:text="1 - 5"
        android:textAlignment="center" />
    <ImageView
        android:id="@+id/icon"
        android:layout_width="16dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:src="@drawable/user"
        android:layout_marginTop="2dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textListRunners"
        android:text="3/5"
        android:layout_marginLeft="10dp"
        android:textSize="16dp" />
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textListTime"
        android:text="-3:59"
        android:textSize="16dp"
        android:gravity="right" />

</LinearLayout>

,此处呈现在设备上的RecyclerView

recyclerview

非常感谢。

首先,您将LayoutParams设置在populateViewHolder功能中,这可能会减慢RecyclerView的滚动。为此,您可能有不同的布局,也可以使用相同的布局,只需将可见性设置为GONEVISIBLE即可。

因此,基本上您的布局具有四个TextView。现在设置您的布局。

<LinearLayout ..>
    <TextView 
        android:id="@+id/distance" ../>
    <!-- Add an extra TextView here and set visibility=gone by default -->
    <TextView 
        android:id="@+id/distance_italic"
        android:visibility="gone"
        android:gravity="center" ../>
    <TextView 
        android:id="@+id/level" ../>
    <TextView 
        android:id="@+id/runner" ../>  
    <TextView 
        android:id="@+id/time" ../>
</LinearLayout>

将额外的TextView放在ViewHolder

public static class ViewHolder extends RecyclerView.ViewHolder {
    // ... Other textviews
    private final TextView textListDistanceItalic;
    public ViewHolder(View itemView) {
        super(itemView);
        // ... Initialize others
        textListDistanceItalic = (TextView) itemView.findViewById(R.id.distance_italic);
    }
    // ... Other Setters
    public void setItalicInvisible() {
        textListDistanceItalic.setVisibility(View.GONE);
    }
    public void setItalicVisible() {
        textListDistanceItalic.setVisibility(View.VISIBLE);
    }
}

现在,在您的populateViewHolder功能中,您可能会有此功能。

if(position < 3) {
    // Call the setters
    // ...
    // Set the italic distance invisible
    viewHolder.setItalicInvisible();
} else {
    // Call the setters
    // ...
    // Set the italic distance visible
    viewHolder.setItalicVisible();
}

相关内容

  • 没有找到相关文章

最新更新