Java Android - 来自创建适配器的 NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28414480/
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 - NullPointerException from creating an adapter
提问by JozeRi
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..
我正在为列表视图创建一个数组适配器,一切正常,我有 2 个片段,并且操作栏顶部的 2 个按钮在这 2 个片段之间变化。我的问题是,如果我在这些片段之间移动太快,我会崩溃,当我打开 fragOne 时,切换到 fragTwo,然后快速移回 fragOne.. fragOne 从 getActivity 上下文中抛出一个 NPE..
that's the line that crashes:
那是崩溃的行:
adapter = new MainFragmentDocumentAdapter(getActivity(), docsList, DocumentsFragment.this, page);
the log report :
日志报告:
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?
知道如何解决这个问题吗?
采纳答案by JozeRi
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.
所以基本上经过大量检查后,我发现问题是我在另一个片段中返回到那个片段,因为我有一个监听器指出那里并试图打开那个方法。
basically I just wrapped it in a
基本上我只是把它包在一个
if (getActivity() != null) {
// Code goes here.
}
and problem solved.
并解决了问题。
thanks a lot though for all the help guys !
非常感谢所有帮助家伙!
回答by Shyantan deb
Use try catch block, it worked for me
使用 try catch 块,它对我有用
回答by Srishti Roy
you can do like this.
你可以这样做。
inside activity
内部活动
public static UsageRecommendationTabActivity getInstance() {
return activityInstance;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityInstance=this;
}
inside fragment
内部片段
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
if (activity instanceof UsageRecommendationTabActivity)
mParentActivity = (UsageRecommendationTabActivity) activity;
if (mParentActivity == null)
mParentActivity = UsageRecommendationTabActivity.getInstance();
}
and then call your adapter
然后调用你的适配器
adapter = new MainFragmentDocumentAdapter(mParentActivity, docsList, DocumentsFragment.this, page);
回答by Pedro Rom?o
if you are on a Fragment check if the fragment is added.
如果您在 Fragment 检查是否添加了 Fragment。
{
...
if (!this.isAdded()) { //this = current fragment
return;
}
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
...
}
回答by Amir Dora
Add checking for getContext()!=null
添加对 getContext()!=null 的检查
Before block
块前
mAdapter = new SimpleHomeItemAdapter(getActivity(), mList);
listview.setAdapter(mAdapter);
For example:
例如:
if (getActivity()!=null){
mAdapter = new SimpleHomeItemAdapter(getActivity(), mList);
listview.setAdapter(mAdapter);
}