getApplicationContext给出空指针异常[重复]

少年意气强不羁,虎胁插翼白日飞。这篇文章主要讲述getApplicationContext给出空指针异常[重复]相关的知识,希望能为你提供帮助。
这个问题在这里已有答案:

  • What is a NullPointerException, and how do I fix it? 12个答案
我有一个片段,通过引用从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。将子视图添加到空视图时会出错。
【getApplicationContext给出空指针异常[重复]】应该是正确的线
mExpandableView = (ExpandablePlaceHolderView) rootView.findViewById(R.id.expandableView);


    推荐阅读