代码之家  ›  专栏  ›  技术社区  ›  user9347089

Android Stackoverflow错误:ViewGroup。可跳转到当前状态

  •  1
  • user9347089  · 技术社区  · 6 年前

    我有个问题。我有一个Stackoverflow错误。我试着缩小布局等等。但什么都没用。

    我有一个查看寻呼机,在片段中有4次是recyclerviews。(各一个)。充气机代码

    View root = inflater.inflate(R.layout.content_main, container, false);
    

    片段中的一个布局,例如:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/list_view"
        xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_margin="10dp"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content" />
    

    这是Recerview项的布局

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        android:id="@+id/cv"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_margin="20dp"
        android:clickable="true"
        android:background="?attr/selectableItemBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center|left"
            android:padding="10dp"
            android:clickable="true"
            android:background="?attr/selectableItemBackground"
            >
    
            <ImageView
                android:id="@+id/album_cover"
                android:layout_width="33dp"
                android:layout_height="50dp"
                android:layout_marginRight="16dp"
                android:src="@drawable/ic_supervisor_account_black_24dp" />
    
            <TextView
                android:id="@+id/album_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Artistname"
                android:textColor="#000000"
                android:textSize="15sp" />
    
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    

    一个布局有一个相对布局,但在我进行一些更改之前它可以工作:

     <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/cv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:background="?attr/selectableItemBackground"
            android:layout_margin="10dp">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:clickable="true"
                android:longClickable="true"
                android:background="?attr/selectableItemBackground"
                >
    
                <ImageView
                    android:id="@+id/album_cover"
                    android:layout_width="66dp"
                    android:layout_height="50dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginRight="16dp"
                    android:src="@drawable/spring_276014_640" />
    
                <TextView
                    android:id="@+id/album_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toRightOf="@+id/album_cover"
                    android:text="Albumtitel"
                    android:textColor="#000000"
                    android:textSize="20sp" />
    
                <TextView
                    android:id="@+id/album_artist"
                    android:layout_width="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/album_name"
                    android:layout_toRightOf="@+id/album_cover"
                    android:text="von Artist xy" />
    
            </RelativeLayout>
    
        </android.support.v7.widget.CardView>
    

    无法捕获视图表,因为应用程序在更改布局之前已关闭。

    位图已缩放。

    这是我的适配器:

    public class DefaultAdapter extends RecyclerView.Adapter<DefaultAdapter.SongViewHolder> {
    private Context con;
    private List<String> names;
    boolean useplaylist;
    
    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class SongViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView title;
    
        SongViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            title = (TextView)itemView.findViewById(R.id.album_name);
        }
    }
    
    // Provide a suitable constructor (depends on the kind of dataset)
    public DefaultAdapter(Context context,List<String> names,boolean playlist) {
        con = context;
        this.names = names;
        useplaylist = playlist;
    }
    
    // Create new views (invoked by the layout manager)
    @Override
    public SongViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.playlist_item, viewGroup, false);
    
    
    
        SongViewHolder pvh = new SongViewHolder(v);
        return pvh;
    }
    
    @Override
    public void onBindViewHolder(SongViewHolder personViewHolder, int i) {
        personViewHolder.title.setText(names.get(i));
    }
    
    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return names.size();
    }
    
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }
    }
    

    日志:

    java.lang.StackOverflowError: stack size 8MB\n
        at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6108)
        at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6112)
        at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6112)
        at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6112)
        at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6112)
        at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6112)
    

    谁能帮帮我吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   user9347089 user9347089    6 年前

    问题已解决。

    我不知道错误在哪里,但可能是一个空列表或ImageAssets,我将其替换为vector assets并激活Gradle中的库。

    对于那些在viewpage中有相同问题的人:停用所有片段并单独启用它们,以测试错误在哪里。