为什么我收到错误:java.lang.RuntimeException:java.lang.IllegalStateException:RecyclerView没有LayoutManager(HelpActivity.java:28)

时间:2015-10-22 07:11:04

标签: java android material-design android-recyclerview android-xml

我正在开发材料设计应用。我想在RecyclerView中使用HelpActivity显示一个列表。

问题是我收到了以下错误:第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>

我无法弄清问题是什么。

请告诉我。

提前致谢。

2 个答案:

答案 0 :(得分:0)

您不能将您的子元素直接放在回收者视图中 您需要创建单独的行布局,就像您在listview中创建的那样。

只需删除在recycleler视图中编写的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);

答案 1 :(得分:0)

我只是对你的content_help.xml进行了一些小改动,它对我来说很好用.. content_help.xml

&

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

希望这能解决您的问题