Android的滑动菜单getActivity返回null [英] android sliding menu getActivity returns null

查看:325
本文介绍了Android的滑动菜单getActivity返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这一段时间,并没有发现任何解决方案,到目前为止,

I've been searching for this for a while now and haven't found any solution so far,

所以我有一个滑动菜单,并withing它,我称之为 getActivity(),到目前为止好,但是当我走出应用(Home键)和回来几分钟,在 getActivity()收益 NullPointerException异常

so I have a sliding menu, and withing it I call getActivity(), so far so good, but when I get out of the app (home button) and come back in a few minutes, the getActivity() returns nullpointerexception

如果任何人有一个关于这条线索将是pciated多少AP $ P $

if anyone has a clue about this it would be much appreciated

感谢

推荐答案

您必须小心究竟当你调用getActivity()。它会在()通过onResume由父活动的生命周期触发一路任何调用返回null。你必须确保它已经连接到父活动之前,您可以安全地调用它。看看

You have to be careful about exactly when you call getActivity(). It will return null in any call triggered by the parent activity's lifecycle all the way through onResume(). You have to make sure it is already attached to the parent activity before you can safely call it. Take a look at

<一个href=\"http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html#onResumeFragments()\" rel=\"nofollow\">http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html#onResumeFragments()

也许它会帮助你解决你的问题。

Perhaps it will help you resolve your problem.

根据你想做什么,我会考虑节约指针到活动中的片段,因此有时被引用时getActivity可能为空。也就是说,这可能是有点危险,所以要小心,如果这是你选择乘坐路线

Depending on what you're trying to do, I would look into saving a pointer to the activity in the fragment so it can be referenced at times when getActivity might be null. That said, this could be kinda dangerous so be careful if that is the route you choose to take

这篇关于Android的滑动菜单getActivity返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆