gpt4 book ai didi

java - 尝试调用虚拟方法 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' android

转载 作者:行者123 更新时间:2023-11-29 19:10:59 27 4
gpt4 key购买 nike

我在设置 RecyclerViewAdapter 时遇到了一些问题

我收到错误

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' on a null object reference

mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());

如何修复

for (ProfileFeedListModelTwo.PostBean.PollBean pollBean : pp){
pollBeanArrayList.add(pollBean);}

adapDetailTopicPoll = new AdapDetailTopicPoll(pollBeanArrayList,getContext());
mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapDetailTopicPoll);

最佳答案

getActivity()null。您可能在将 Activity 附加到 fragment 之前调用此方法。

来自documentation :

Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() will return null

尝试理解 fragment lifecycle以及它是如何与 activity lifecycle 结合的.

关于java - 尝试调用虚拟方法 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45334326/

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