Android的 - 从的NullPointerException创建适配器 [英] Android - NullPointerException from creating an adapter

查看:234
本文介绍了Android的 - 从的NullPointerException创建适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个数组适配器列表视图,
一切工作正常,我有2个片段,和2个按钮在这2片段之间改变了操作栏的顶部。
我的问题是,我得到的崩溃,如果我太快那些断肢之间,当我打开fragOne,切换到fragTwo,然后迅速搬回fragOne .. fragOne从getActivity背景下..

I am creating an array adapter for a list view, everything works ok, I have 2 fragments, and 2 buttons at the top of the action bar that changes between this 2 fragments. my problem is that I get crashes if I move too fast between those frags, when I open fragOne, switch to fragTwo, and then quickly move back to fragOne.. fragOne throws a NPE from the getActivity context..

这是该崩溃的行:

adapter = new MainFragmentDocumentAdapter(getActivity(), docsList, DocumentsFragment.this, page);

日志报告:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bbb.app, PID: 17438
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
        at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
        at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:104)
        at com.bbb.app.UI.adapters.MainFragmentDocumentAdapter.<init>(MainFragmentDocumentAdapter.java:51)

任何想法,我怎么能解决这个问题?

any idea how can I solve this issue?

推荐答案

所以基本上很多检查后我发现的问题是,我要回那个片段,而不同的片段里面,因为我有一个听众指出有并试图打开该方法。

So basically after lots of check I found out that the problem was that I was returning to that fragment while inside a different fragment because I had a listener pointing out there and trying to open that method.

基本上我只是把它包在

if (getActivity() != null) {
    // Code goes here.
}

和问题解决了。

虽然非常感谢所有帮助家伙!

thanks a lot though for all the help guys !

这篇关于Android的 - 从的NullPointerException创建适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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