getApplicationContext给出空指针异常[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了getApplicationContext给出空指针异常[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我有一个片段,通过引用从Utils类获取数据。但是,当我运行我的代码时,我得到一个空指针异常
--------- beginning of crash
03-20 17:17:18.859 3100-3100/? E/androidRuntime: FATAL EXCEPTION: main
Process: uk.ac.napier.newsreader, PID: 3100
java.lang.NullPointerException: Attempt to invoke virtual method 'com.mindorks.placeholderview.PlaceHolderView com.mindorks.placeholderview.ExpandablePlaceHolderView.addView(java.lang.Object)' on a null object reference
at uk.ac.napier.newsreader.Nav_Drawer.NewsFragment.onCreateView(NewsFragment.java:39)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
03-20 17:17:18.861 1588-2193/system_process W/ActivityManager: Force finishing activity uk.ac.napier.newsreader/.MainActivity
导致这种情况的代码就是这一行
mExpandableView.addView(new HeadingView(mContext, feed.getHeading()));
这是我的整个片段
/**
* A simple {@link Fragment} subclass.
*/
public class NewsFragment extends Fragment {
private ExpandablePlaceHolderView mExpandableView;
private Context mContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_news, container, false);
mContext = getActivity().getApplicationContext();
mExpandableView = (ExpandablePlaceHolderView) getActivity().findViewById(R.id.expandableView);
for (Feed feed : Utils.loadFeeds(getActivity().getApplicationContext())) {
mExpandableView.addView(new HeadingView(mContext, feed.getHeading()));
for (Info info : feed.getInfoList()) {
mExpandableView.addView(new InfoView(mContext, info));
}
}
return rootView;
}
}
我不允许在这个实例中使用getActivity()吗?我尝试了getContext,但它引发了更多的错误。任何帮助表示赞赏。
答案
你问的是这一行:
mExpandableView = (ExpandablePlaceHolderView) getActivity().findViewById(R.id.expandableView);
此时mExpandableView
将为null。将子视图添加到空视图时会出错。
应该是正确的线
mExpandableView = (ExpandablePlaceHolderView) rootView.findViewById(R.id.expandableView);
以上是关于getApplicationContext给出空指针异常[重复]的主要内容,如果未能解决你的问题,请参考以下文章
getContext() 、 getApplicationContext() 、 getBaseContext() 和“this”之间的区别
为啥我得到 getApplicationcontext() null?
requireActivity().getApplicationContext() “可能会产生 NullPointerExeption”
Monodroid getApplicationContext()?
getContext() , getApplicationContext() , getBaseContext()的区别
区别以及何时使用 getApplication()、getApplicationContext()、getBaseContext() 和 someClass.this