当前位置: 代码迷 >> Android >> 为什么会出现错误:java.lang.RuntimeException:java.lang.IllegalStateException:RecyclerView上没有(HelpActivity.java:28)LayoutManager
  详细解决方案

为什么会出现错误:java.lang.RuntimeException:java.lang.IllegalStateException:RecyclerView上没有(HelpActivity.java:28)LayoutManager

热度:86   发布时间:2023-08-04 11:05:53.0

我正在开发材质设计应用程序。 我想在HelpActivity中使用RecyclerView显示列表。

问题是我遇到以下错误: java.lang.RuntimeException: java.lang.IllegalStateException: RecyclerView has no LayoutManager在行java.lang.RuntimeException: java.lang.IllegalStateException: RecyclerView has no LayoutManager (HelpActivity.java:28)

这是HelpActivity.java文件的代码:

public class HelpActivity extends AppCompatActivity {

    public RecyclerView mRecyclerView;
    public RecyclerView.LayoutManager mLayoutManager;
    public RecyclerView.Adapter mAdapter;

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

        mRecyclerView = (RecyclerView) findViewById(R.id.helpActivityContent);

        // use a linear layout manager
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        mAdapter = new HelpContentAdapter(helpContents);
        mRecyclerView.setAdapter(mAdapter);

        initializeData();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.parseColor("#2196F3"));
        setSupportActionBar(toolbar);

        /*final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        upArrow.setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.SRC_ATOP);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(upArrow);*/

        /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });*/
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    class HelpActivityContent {
        String option;
        String subOption;
        int icon;

        HelpActivityContent(String option, String subOption, int icon) {
            this.option = option;
            this.subOption = subOption;
            this.icon = icon;
        }
    }

    public List<HelpActivityContent> helpContents;

    // This method creates an ArrayList that has three Person objects
// Checkout the project associated with this tutorial on Github if
// you want to use the same images.
    private void initializeData(){
        helpContents = new ArrayList<>();
        helpContents.add(new HelpActivityContent("123", "", R.drawable.ic_action_a));
        helpContents.add(new HelpActivityContent("123", "", R.drawable.ic_action_b));
        helpContents.add(new HelpActivityContent("321", "111", R.drawable.ic_action_c));
        helpContents.add(new HelpActivityContent("1", "2", R.drawable.ic_action_d));
    }

    public class HelpContentAdapter extends RecyclerView.Adapter<HelpContentAdapter.HelpContentViewHolder>{

        public class HelpContentViewHolder extends RecyclerView.ViewHolder {
            TextView option;
            TextView subOption;
            ImageView icon;

            HelpContentViewHolder(View itemView) {
                super(itemView);
                option = (TextView)itemView.findViewById(R.id.option);
                subOption = (TextView)itemView.findViewById(R.id.subOption);
                icon = (ImageView)itemView.findViewById(R.id.optionIcon);
            }

        }

        List<HelpActivityContent> helpContents;

        HelpContentAdapter(List<HelpActivityContent> helpContents){
            this.helpContents = helpContents;
        }

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

        @Override
        public HelpContentViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.content_help, viewGroup, false);
            HelpContentViewHolder pvh = new HelpContentViewHolder(v);
            return pvh;
        }

        @Override
        public void onBindViewHolder(HelpContentViewHolder helpContentViewHolder, int i) {
            helpContentViewHolder.option.setText(helpContents.get(i).option);
            helpContentViewHolder.subOption.setText(helpContents.get(i).subOption);
            helpContentViewHolder.icon.setImageResource(helpContents.get(i).icon);
        }

        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        switch (id) {
            case android.R.id.home:
                supportFinishAfterTransition();
                return true;
        }

        return super.onOptionsItemSelected(item);

    }

}

这是content_help.xml文件的代码:

<?xml version="1.0" encoding="utf-8"?>

<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/helpActivityContent"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_help"
    tools:context="com.abc.xyz.HelpActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/optionIcon"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="16dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/option"
        android:layout_toRightOf="@+id/optionIcon"
        android:layout_alignParentTop="true"
        android:textSize="30sp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/subOption"
        android:layout_toRightOf="@+id/optionIcon"
        android:layout_below="@+id/option"
        />

</RelativeLayout>
</android.support.v7.widget.RecyclerView>

我无法找出问题所在。

请告诉我。

提前致谢。

您不能将子元素直接放在回收器视图中。
您需要创建单独的行布局,例如在listview中创建的行布局。

只需删除在回收器视图中编写的xml代码,然后将其插入row_layout xml文件中,然后将该行布局文件插入适配器中即可。

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_help"
tools:context="com.abc.xyz.HelpActivity">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/optionIcon"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="16dp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/option"
    android:layout_toRightOf="@+id/optionIcon"
    android:layout_alignParentTop="true"
    android:textSize="30sp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/subOption"
    android:layout_toRightOf="@+id/optionIcon"
    android:layout_below="@+id/option"
    />

</RelativeLayout>

只需为包含以上内容的row_layout创建一个单独的文件,然后在您的oncreateviewholder内像下面那样对其进行充气

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.**<new_layout_you_will_create>**, viewGroup, false);

我只是在您的content_help.xml中做了一个小小的改动,对我来说很好用。

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_help"
tools:context="com.abc.xyz.HelpActivity">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/optionIcon"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="16dp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/option"
    android:layout_toRightOf="@+id/optionIcon"
    android:layout_alignParentTop="true"
    android:textSize="30sp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/subOption"
    android:layout_toRightOf="@+id/optionIcon"
    android:layout_below="@+id/option"
    />

如果您在activity_help.xml中使用recyclerview,则无需对行内容使用相同的内容。

希望这能解决您的问题

  相关解决方案