getApplicationContext给出空指针异常

时间:2017-03-20 17:27:41

标签: java android nullpointerexception

我有一个片段,它通过引用从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,但它引发了更多的错误。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

你问题就在这一行:

mExpandableView = (ExpandablePlaceHolderView) getActivity().findViewById(R.id.expandableView);
此时

mExpandableView将为null。将子视图添加到空视图时会出错。

正确的行应

mExpandableView = (ExpandablePlaceHolderView) rootView.findViewById(R.id.expandableView);