gpt4 book ai didi

java - Android:RecyclerView 空指针异常

转载 作者:行者123 更新时间:2023-12-02 08:39:10 27 4
gpt4 key购买 nike

我已经创建了回收器 View ,并且已在 Gradle 文件中导入了所有必要的依赖项。我的目标 SDK 是 29.所以我所有的依赖都是 androidx在我的布局文件中,我还创建了“list”的 id,但我不知道为什么它显示错误。预先感谢您。

package com.napps.wallpaper;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class fragment_wallpaper extends Fragment {

RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
RecyclerView.Adapter adapter;
View view;

public fragment_wallpaper(){

}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view= inflater.inflate(R.layout.fragment_wallpaper,container,false);
return view;

}


@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);


recyclerView=view.findViewById(R.id.list); //this where i am getting error
recyclerView.setHasFixedSize(true);

layoutManager=new LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(layoutManager);

adapter=new imageAdapter(this.getActivity(),array_class.arrayurl);
recyclerView.setAdapter(adapter);

}

}



最佳答案

试试这个
基本上,您没有初始化 View ,而是创建了一个导致麻烦的新变量。希望能帮助到你。快乐编码:)

        @Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

view= inflater.inflate(R.layout.fragment_wallpaper,container,false);
return view;

}

关于java - Android:RecyclerView 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61495361/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com