java - LinearLayoutManager 不能在 Fragment 中使用

标签 java android-studio

我的 LinearLayoutManager 有一个错误,当我运行这段代码时它说

error: incompatible types: NotificationFragment cannot be converted to Context

这是我的代码 NotificationFragment.java

public class NotificationFragment extends Fragment {

    private RecyclerView recyclerView;
    private NotificationAdapter adapter;
    private ArrayList<NotificationModel> notificationModelArrayList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_notification,container,false);

        addData();

        recyclerView = (RecyclerView)   getView().findViewById(R.id.recycler_view);

        adapter = new NotificationAdapter(notificationModelArrayList);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(NotificationFragment.this);

        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setAdapter(adapter);
    }

    void addData(){
        notificationModelArrayList = new ArrayList<>();
        notificationModelArrayList.add(new NotificationModel("Event 1", "1 January 2019", "Surabaya"));
        notificationModelArrayList.add(new NotificationModel("Event 2", "1 January 2019", "Surabaya"));
        notificationModelArrayList.add(new NotificationModel("Event 3", "1 January 2019", "Surabaya"));
        notificationModelArrayList.add(new NotificationModel("Event 4", "1 January 2019", "Surabaya"));
    }

}

出现错误

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(NotificationFragment.this);

希望你能帮助我,谢谢。

最佳答案

您不能将 Fragment 转换为 Context

编辑这一行:

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(NotificationFragment.this);

到:

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());

请记住在使用前为 getContext()getActivity() 检查 null。

对于下面评论中的新问题,只需将 return 语句置于函数末尾即可:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_notification,container,false);

    addData();

    recyclerView = (RecyclerView)  view.findViewById(R.id.recycler_view);

    adapter = new NotificationAdapter(notificationModelArrayList);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(NotificationFragment.this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

    return view;
}

关于java - LinearLayoutManager 不能在 Fragment 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54003985/

相关文章:

java - 公开远程接口(interface)或对象模型

java.sql.SQLException : Can not issue data manipulation statements with executeQuery() 异常

JAVA,比较两个字符串

java - 在同一部手机上运行应用程序时,Android Studio 生成的 APK 崩溃工作正常。怎么了?

android - Gradle 无法解析 androidx.appcompat :appcompat:1. 1.0-alpha01 和 com.google.android.gms :play-services-nearby:16. 0.0

Java - 获取 JSONObject 时出现 NullPointerException

java - 如何查找对象引用的多维数组的数组类型及其维度

android - 如何在 android studio 中启用堆栈跟踪?它的窗口在哪里?

android - Android Studio 模拟器上的屏幕闪烁

c - 在 Android Studio 中添加 MATLAB CODER 生成的 C