当前位置: 首页 > 面试题库 >

如果有一个为什么说“ RecyclerView没有LayoutManager”。请查看详细信息

沈国安
2023-03-14
问题内容

recyclerview在我的应用程序中做了一个

问题是,java.lang.IllegalStateException: RecyclerView has no LayoutManager即使定义了错误,我也会收到以下错误:

这是ListContentAAR.java文件的代码

class ListContentAAR {
    String picTag;

    ListContentAAR( String picTag) {
        this.picTag = picTag;
    }

}

这是RVAdapterAAR.java文件的代码

public class RVAdapterAAR extends RecyclerView.Adapter<RVAdapterAAR.PersonViewHolder> {

    public static class PersonViewHolder extends RecyclerView.ViewHolder {

        CardView cardView;
        TextView pic;

        PersonViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.card_accept_request);
            homelessPic = (TextView) itemView.findViewById(R.id.pic_tag);
        }

    }

    List<ListContentAAR> listContentAARs;

    RVAdapterAAR(List<ListContentAAR> listContentAARs) {
        this.listContentAARs = listContentAARs;
    }

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

    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_accept_a_request, viewGroup, false);
        PersonViewHolder personViewHolder = new PersonViewHolder(view);
        return personViewHolder;
    }

    public void onBindViewHolder (PersonViewHolder personViewHolder, int i) {
        personViewHolder.pic.setText(listContentAARs.get(i).homelessPicTag);
    }

    public int getItemCount() {
        return listContentAARs.size();
    }

}

这是AcceptARequest.java文件的代码

public class AcceptARequest extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    private List<ListContentAAR> listContentAARs;
    private RecyclerView recyclerView;

    public AcceptARequest() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment AcceptARequest.
     */
    // TODO: Rename and change types and number of parameters
    public static AcceptARequest newInstance(String param1, String param2) {
        AcceptARequest fragment = new AcceptARequest();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View rootView = inflater.inflate(R.layout.fragment_accept_a_request, container, false);

        RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.accept_request_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);

        initializeData();
        initializeAdapter();

        return rootView;
    }

    private void initializeData(){
        listContentAARs = new ArrayList<>();
        listContentAARs.add(new ListContentAAR("Emma Wilson"));
    }

    private void initializeAdapter(){
        RVAdapterAAR adapter = new RVAdapterAAR(listContentAARs);
        recyclerView.setAdapter(adapter);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

这是fragment_accept_a_request.xml文件的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                tools:context="com.abc.xyz.AcceptARequest">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/accept_request_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.CardView
            android:id="@+id/card_accept_request"
            android:layout_width="match_parent"
            android:layout_height="@dimen/card_accept_request"
            app:cardElevation="2dp"
            app:cardUseCompatPadding="true"
            app:contentPadding="10dp">

            <!--<ImageView
                android:id="@+id/pic_accept"
                android:layout_width="match_parent"
                android:layout_height="@dimen/pic_dimen_accept"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_alignParentTop="true"/> -->

            <TextView
                android:id="@+id/pic_tag"
                android:layout_width="match_parent"
                android:layout_height="@dimen/pic_dimen_accept"
                android:text="@string/image_view_tag_accept"
                android:gravity="center_horizontal|center_vertical"/>

        </android.support.v7.widget.CardView>

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

</RelativeLayout>

这是MainActivity.java文件的代码

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    public SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

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

        Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/Pacifico.ttf");

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
        mTitle.setTypeface(typeFace);
        toolbar.setTitle(mTitle.getText().toString());
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        /*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();
            }
        });*/

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
            startActivity(settingsIntent);
        } else if (id == R.id.action_profile) {
            Intent profileIntent = new Intent(MainActivity.this, ProfileActivity.class);
            startActivity(profileIntent);
        } else if (id == R.id.action_send_feedback) {
            Intent sendFeedbackIntent = new Intent(MainActivity.this, SendFeedback.class);
            startActivity(sendFeedbackIntent);
        } else if (id == R.id.action_help) {
            Intent helpIntent = new Intent(MainActivity.this, HelpActivity.class);
            startActivity(helpIntent);
        }

        return super.onOptionsItemSelected(item);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        protected Context mContext;

        public SectionsPagerAdapter(Context context, FragmentManager fm) {
            super(fm);
            mContext = context;
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            // return MainActivity.PlaceholderFragment.newInstance(position + 1);

            switch (position) {
                case 0:
                    return new AcceptARequest();
                case 1:
                    return new PostARequest();
                default:
                    return new AcceptARequest(); // return AcceptARequest() actually
            }
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return "Accept A Request";
                case 1:
                    return "Post A Request";
            }
            return null;
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }
}

这是stacktrace

FATAL EXCEPTION: main
                                                                               java.lang.IllegalStateException: RecyclerView has no LayoutManager
                                                                                   at android.support.v7.widget.RecyclerView.generateLayoutParams(RecyclerView.java:3182)
                                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:748)
                                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                                                   at com.humanehelper.humanehelper.AcceptARequest.onCreateView(AcceptARequest.java:75)
                                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                                   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                                   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                                   at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                                                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                                                   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
                                                                                   at android.view.View.measure(View.java:15172)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
                                                                                   at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
                                                                                   at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)
                                                                                   at android.view.View.measure(View.java:15172)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                                   at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                                   at android.view.View.measure(View.java:15172)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
                                                                                   at android.view.View.measure(View.java:15172)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                                   at android.view.View.measure(View.java:15172)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
                                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
                                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
                                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
                                                                                   at android.view.View.measure(View.java:15172)
                                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
                                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                                   at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
                                                                                   at android.view.View.measure(View.java:15172)
                                                                                   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
                                                                                   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
                                                                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
                                                                                   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
                                                                                   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
                                                                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
                                                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:555)
                                                                                   at android.view.Choreographer.doFrame(Choreographer.java:525)
                                                                                   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
                                                                                   at android.os.Handler.handleCallback(Handler.java:615)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                   at android.os.Looper.loop(Looper.java:137)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                at com

由于我是初学者,所以无法弄清楚为什么会出现以下错误。

请告诉我。

提前致谢。


问题答案:

我知道了!

问题出在两个文件中。

劳拉托所说的第一个问题是fragment_accept_a_request.xml。从XML元素中删除XML元素recyclerView并将其放置在其下方,但使用相同的父布局完成了这项工作。

第二个问题是AcceptARequest.java。在这里我声明了两个RecyclerView一个是initialiseData()方法和另一个用于声明recyclerView

我所做的更改fragment_accept_a_request.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                tools:context="com.abc.xyz.AcceptARequest">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/accept_request_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

    <android.support.v7.widget.CardView
        android:id="@+id/card_accept_request"
        android:layout_width="match_parent"
        android:layout_height="@dimen/card_accept_request"
        app:cardElevation="2dp"
        app:cardUseCompatPadding="true"
        app:contentPadding="10dp">

        <!--<ImageView
            android:id="@+id/pic_accept"
            android:layout_width="match_parent"
            android:layout_height="@dimen/pic_dimen_accept"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_alignParentTop="true"/> -->

        <TextView
            android:id="@+id/pic_tag"
            android:layout_width="match_parent"
            android:layout_height="@dimen/pic_dimen_accept"
            android:text="@string/image_view_tag_accept"
            android:gravity="center_horizontal|center_vertical"/>

    </android.support.v7.widget.CardView>

</RelativeLayout>


 类似资料:
  • 问题内容: 我正在探索,惊讶地发现那没有。 我有两个问题。 主要问题 我想知道为什么删除了? 是否存在性能问题或其他问题? 次要问题 我解决我的问题写我的: 这样可以/有更好的方法吗? 问题答案:

  • “复制详细信息”页面显示你在受监控的复制页面中选择的从服务器或订阅服务器的详细复制信息。 复制详细信息页面的信息 摘要 本页面的开始部分列出了从服务器和主服务器信息。对于 MySQL 或 MariaDB 多源复制,你可以点击主服务器名来查看其复制详细信息。 复制启动关闭图表 它显示复制的启动或关闭状态图表或成功失败状态图表。时间选择器用于选择图表内显示的时间跨度和间隔(X 轴)。它包含日期时间选取

  • 在概览页面中,点击一个实例卡片以打开其“实例详细信息”页面。它以可视方式显示服务器参数和指标,让你快速查看服务器负载和性能。你可以使用“暂停监控”和“继续监控”按钮来暂停和继续监控实例。Navicat Monitor 会停止从服务器收集信息,直到监控继续。 所有受监控的实例都显示在左侧窗格中。选择一个实例以查看与其相关的信息。 实例详细信息页面的信息 摘要 它显示有关服务器的主机信息、服务器属性、

  • 本文向大家介绍请你说一说有了进程,为什么还要有线程?相关面试题,主要包含被问及请你说一说有了进程,为什么还要有线程?时的应答技巧和注意事项,需要的朋友参考一下 参考回答: 线程产生的原因: 进程可以使多个程序能并发执行,以提高资源的利用率和系统的吞吐量;但是其具有一些缺点: 进程在同一时间只能干一件事 进程在执行的过程中如果阻塞,整个进程就会挂起,即使进程中有些工作不依赖于等待的资源,仍然不会执行

  • 如果不存在要显示的数据,我想禁用详细信息控制。 HTML: 如果位置1上没有数据显示,我如何隐藏显示图标?我咨询这个网站:https://datatables.net/examples/api/row_details.html查看api和它是如何工作的。 数据表API有许多方法用于将子行附加到数据表中的父行。这可用于显示有关行的其他信息,对于希望传递有关行的信息超过主机表空间的情况很有用。 下面的

  • 本文向大家介绍请你来详细说明一下微信红包收发的详细过程。相关面试题,主要包含被问及请你来详细说明一下微信红包收发的详细过程。时的应答技巧和注意事项,需要的朋友参考一下 我们先将涉及人员分为发送方和接收方。微信红包收发具体流程如下(先讨论一对一的红包); 1.发送方在接收方的对话框里点击红包选项 2.发送方填写红包金额 3.发送方填写祝福话语(选填) 4.发送方点击确认并选择金额来源(零钱,银行卡等