简体   繁体   English

应用恢复时片段中的getActivity null

[英]getActivity null in fragment when app resumes

So if the user is on my app and they click home and go to several other apps and then come back, then the activity will be recreated and getActivity will be null when I call on it in my fragment. 因此,如果用户在我的应用程序上,然后单击主页并转到其他几个应用程序,然后再返回,则将重新创建该活动,并且当我在片段中对其进行调用时, getActivity将为null。

A solution I found was to create a static variable and store getActivity in the onCreateView . 我发现的一个解决方案是创建一个静态变量并将getActivity存储在onCreateView

I feel like this isn't a good solution. 我觉得这不是一个好的解决方案。 Is there any other way that I can go about this? 我还有其他方法可以解决这个问题吗?

I tried using a non static variable and storing it in OncreateView and onAttach, but getActivity will be null. 我尝试使用非静态变量并将其存储在OncreateView和onAttach中,但getActivity将为null。

Here is the error I will get when I use getActivity if I don't save it as a static variable. 如果不将其保存为静态变量,这就是使用getActivity时出现的错误。 I use it in my AsyncTask for ProcessDialog in my Fragment. 我在Fragment的ProcessDialog的AsyncTask使用它。

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference

Thanks. 谢谢。

If you are sure that onAttach(Activity activity) also has null, then I suspect you have multiple instance of same fragment at the same time. 如果您确定onAttach(Activity activity)也为null,那么我怀疑您同时有多个相同片段的实例。 Print the fragment instance in onResume and check the instances. 在onResume中打印片段实例并检查实例。

Are you implementing the method onActivityCreated ? 您是否正在实现onActivityCreated方法? You should use getActivity inside this method. 您应该在此方法内使用getActivity。

Another workaround would be use onAttach to keep your Activity. 另一个解决方法是使用onAttach来保持您的活动。

@Override
public void onAttach(Activity activity) {
 super.onAttach(activity);
 mActivity = activity;
}

暂无
暂无

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

相关问题 当我在 Fragment 中创建 ImageView 时,如何解决 getActivity() 为 null - How to resolve that getActivity() is null, when I create an ImageView in Fragment onCreateView中的Android片段getActivity()。getApplicationContext()为null - Android Fragment getActivity().getApplicationContext() in onCreateView is null 在Fragment中调用getActivity()返回null - Calling getActivity() inside a Fragment returns null 使用 getactivity() 或 getcontext() 从片段到适配器时获取 null object 参考 - Getting null object reference when using getactivity() or getcontext() from fragment to adapter 当应用程序在后台 bing 后恢复时,我的 Activity 出现 null object 异常 - My Activity is getting null object exception when the app resumes after bing in the background 使用viewpager时,getActivity是否可以返回null? - Can getActivity return null when using a viewpager? getActivity为空时如何终止线程 - How to terminate the Thread when getActivity becomes null 片段中的getActivity()返回null。 父活动中的FragmentPagerAdapter是否有问题? - getActivity() in fragment returns null. Issue with FragmentPagerAdapter in parent activity? 方法调用 getActivity().getSupportFragmentManager() 可能会在片段中产生空指针异常 - method invocation getActivity().getSupportFragmentManager() may produce null pointer exception in fragment getActivity() 或 getContext() 用于带有 viewpager 的片段? - getActivity() or getContext() for a fragment with a viewpager?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM