简体   繁体   English

在 Fragment 中使用 RecyclerView 而不是 Activity 会导致 E/RecyclerView: No adapter attached; 跳过布局错误

[英]Using a RecyclerView inside a Fragment instead of an Activity results in E/RecyclerView: No adapter attached; skipping layout error

I initially created and used a RecyclerView inside my CarActivity class. This worked fine, retrieved the information from the database and displayed it in the view correctly.我最初在我的 CarActivity class 中创建并使用了 RecyclerView。这工作正常,从数据库中检索信息并将其正确显示在视图中。 I then modified the CarActivity class to instead use a new fragment called AllCarsFragment, and moved the RecyclerView code into the new AllCarsFragment.然后,我修改了 CarActivity class,改为使用名为 AllCarsFragment 的新片段,并将 RecyclerView 代码移至新的 AllCarsFragment 中。

The CarActivity seems to pick up the layout of the new fragment correctly, however, no data is displayed and LogCat shows the following error: E/RecyclerView: No adapter attached; skipping layout CarActivity 似乎正确地拾取了新片段的布局,但是,没有显示任何数据并且 LogCat 显示以下错误: E/RecyclerView: No adapter attached; skipping layout E/RecyclerView: No adapter attached; skipping layout . E/RecyclerView: No adapter attached; skipping layout

I've tried moving the code between the fragments onCreateView and onViewCreated methods and messing with the code in a few other ways, however I can't figure out the fix.我试过在片段 onCreateView 和 onViewCreated 方法之间移动代码,并以其他几种方式弄乱代码,但我无法找出解决方法。

I have also browsed similar questions on StackOverflow, but I'm not getting anywhere, so any help is appreciated.我也在 StackOverflow 上浏览过类似的问题,但我没有得到任何帮助,因此不胜感激。

CarActivity.java CarActivity.java

public class CarActivity extends AppCompatActivity {

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

AllCarsFragment.java AllCarsFragment.java

public class AllCarsFragment extends Fragment {
    private static final String LOG_TAG = AllCarsFragment.class.getSimpleName();
    private CarViewModel mCarViewModel;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_all_cars, container, false);

        // Set up the recycler view to display the users saved cars
        RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
        final CarListAdapter adapter = new CarListAdapter(view.getContext());
        recyclerView.setAdapter(adapter);

        mCarViewModel = ViewModelProviders.of(this).get(CarViewModel.class);
        mCarViewModel.getAllCars().observe(getViewLifecycleOwner(), new Observer<List<Car>>() {
            @Override
            public void onChanged(@Nullable final List<Car> cars) {
                // Update the cached copy of the cars in the adapter.
                adapter.setCars(cars);
            }
        });

        return view;
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Configure the FAB to redirect the user to add a new car
        FloatingActionButton fab = view.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(LOG_TAG, "Add Car FAB Pressed");
                NavHostFragment.findNavController(AllCarsFragment.this)
                        .navigate(R.id.add_car_dest, null);
            }
        });
    }
}

Please let me know if you need any further code or info and I'll happily update the question.如果您需要任何进一步的代码或信息,请告诉我,我会很乐意更新问题。

From your CarActivity code, it looks as though you're not actually attaching your AllCarsFragment to your CarActivity in the way that you're intending.从您的 CarActivity 代码看来,您实际上并没有按照预期的方式将 AllCarsFragment 附加到 CarActivity。 The line setContentView(R.layout.fragment_all_cars) sets the layout file as the content view of the CarActivity, but this doesn't hook up your code in AllCarsFragment to the CarActivity. setContentView(R.layout.fragment_all_cars)行将布局文件设置为 CarActivity 的内容视图,但这不会将 AllCarsFragment 中的代码连接到 CarActivity。 To do that, you need some extra code in CarActivity's onCreate() which does the attaching.为此,您需要在 CarActivity 的onCreate()中执行附加的一些额外代码。 In the docs , check out the section on "Adding a fragment to an activity".文档中,查看“将片段添加到活动”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 片段 - E / RecyclerView:没有连接适配器; 跳过布局 - Fragment - E/RecyclerView: No adapter attached; skipping layout E/RecyclerView 错误:未连接适配器; 跳过布局 - Error with E/RecyclerView: No adapter attached; skipping layout E / RecyclerView:没有连接适配器; 跳过布局错误 - E/RecyclerView: No adapter attached; skipping layout error 错误:E / RecyclerView:未连接适配器; 跳过布局 - error: E/RecyclerView: No adapter attached; skipping layout 对RecyclerView的更改导致此错误:E / RecyclerView:未附加适配器; 跳过布局 - Changes to RecyclerView led to this Error :E/RecyclerView: No adapter attached; skipping layout E/RecyclerView:没有附加适配器; 在我的片段中跳过布局 - E/RecyclerView: No adapter attached; skipping layout in my fragment 片段中的RecyclerView - 没有附加适配器,跳过布局 - RecyclerView in Fragment - No Adapter Attached, Skipping Layout E/RecyclerView:没有附加适配器; 在 recyclerview 跳过布局和视图 0 并且不显示 recyclerview - E/RecyclerView: No adapter attached; skipping layout and view 0 at recyclerview and not showing recyclerview E / RecyclerView:未连接适配器; 跳过布局4 - E/RecyclerView: No adapter attached; skipping layout 4 Android:E / RecyclerView:未连接适配器; 跳过布局 - Android: E/RecyclerView: No adapter attached; skipping layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM