繁体   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

我最初在我的 CarActivity class 中创建并使用了 RecyclerView。这工作正常,从数据库中检索信息并将其正确显示在视图中。 然后,我修改了 CarActivity class,改为使用名为 AllCarsFragment 的新片段,并将 RecyclerView 代码移至新的 AllCarsFragment 中。

CarActivity 似乎正确地拾取了新片段的布局,但是,没有显示任何数据并且 LogCat 显示以下错误: E/RecyclerView: No adapter attached; skipping layout E/RecyclerView: No adapter attached; skipping layout

我试过在片段 onCreateView 和 onViewCreated 方法之间移动代码,并以其他几种方式弄乱代码,但我无法找出解决方法。

我也在 StackOverflow 上浏览过类似的问题,但我没有得到任何帮助,因此不胜感激。

CarActivity.java

public class CarActivity extends AppCompatActivity {

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

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);
            }
        });
    }
}

如果您需要任何进一步的代码或信息,请告诉我,我会很乐意更新问题。

从您的 CarActivity 代码看来,您实际上并没有按照预期的方式将 AllCarsFragment 附加到 CarActivity。 setContentView(R.layout.fragment_all_cars)行将布局文件设置为 CarActivity 的内容视图,但这不会将 AllCarsFragment 中的代码连接到 CarActivity。 为此,您需要在 CarActivity 的onCreate()中执行附加的一些额外代码。 文档中,查看“将片段添加到活动”部分。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM