android获取活动返回null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11536166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android get activity returns null
提问by Ashutosh Dubey
I am using Action Bar on an Activity. For each Tab I am showing different layout. Since the layout is too heavy. So I am inflating each layout into a view. So on each Tab select
我在活动上使用操作栏。对于每个选项卡,我显示不同的布局。由于布局太重。所以我将每个布局膨胀到一个视图中。所以在每个选项卡上选择
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mView == null) {
mView = LayoutInflater.from(mAct).inflate(mLayout, null); // mAct is Activity reference
}
mAct.setContentView(mView);
for (int i = 0; i < mFrags.length; i++) {
mFrags[i] = (LutronFragment) mAct.getFragmentManager()
.findFragmentById(mIds[i]);
if (mFrags[i] != null) {
mFrags[i].setupHeader();
}
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
for (Fragment f : mFrags) {
try {
if (f != null) {
ft.remove(f);
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
So Now if I select tab second time and do some operation on that Tab, app get crashed on getActivity.(NullPointerException)
所以现在如果我第二次选择选项卡并在该选项卡上执行一些操作,应用程序会在 getActivity 上崩溃。(NullPointerException)
Please suggest if there is some another approach to cache heavy layout.
请建议是否有其他方法来缓存大量布局。
回答by Mike T
The problem is most likely that you're using an old Fragment that has been detached from your Activity.
问题很可能是您使用的旧 Fragment 已与您的 Activity 分离。
So, the first time you create your Fragment, it is attached to your activity. All is good. Then when you change tab, your fragment might or might not be detached from the activity. When you tab back to it, the old fragment may be detached from the activity and so getActivity()
returns null.
因此,第一次创建 Fragment 时,它会附加到您的活动中。一切都很好。然后,当您更改选项卡时,您的片段可能会或可能不会与活动分离。当您返回到它时,旧片段可能与活动分离,因此getActivity()
返回 null。
This can happen if you're trying to keep references to your Fragments, rather than accessing them via the FragmentManager
.
如果您试图保留对 Fragment 的引用,而不是通过FragmentManager
.
It can also happen if your adapter is returning a reference to a fragment rather than a new fragment. I've fallen into this trap.
如果您的适配器返回对片段而不是新片段的引用,也会发生这种情况。我掉进了这个陷阱。
(Posting the code where you create your fragments might help)
(在您创建片段的位置发布代码可能会有所帮助)
Edit
编辑
Maybe have a look at thisand how they create add their ActionBar listeners. You need scope to your Activity. The way they do it is to define the listener in the Activity/Fragment (via implementing an interface) and then attach it to the Tab. This will give you scope and is probably a more stable way of doing things.
也许看看这个以及他们如何创建添加他们的 ActionBar 侦听器。您需要活动范围。他们这样做的方式是在 Activity/Fragment 中定义侦听器(通过实现一个接口),然后将其附加到 Tab。这将为您提供范围,并且可能是一种更稳定的做事方式。
回答by Kru
This can happen if you create an anonymous object inside a fragment that calls getActiviy()
. If getActivity()
is called in the anonymous object after the fragment is popped off the fragment stack, getActivity()
will return null. At that point, the fragment is no longer associated with an activity.
如果您在调用getActiviy()
. 如果getActivity()
在片段从片段堆栈中弹出后在匿名对象中调用,getActivity()
将返回 null。此时,片段不再与活动相关联。