ListFragment中的NullPointerException设置适配器

时间:2014-05-05 17:23:30

标签: android android-fragments android-listview android-xml android-listfragment

我现在已经超过12个小时,所以也许stackoverflow可以帮助我。

在我的MainActivity中,我有三个从侧面导航抽屉加载的片段。在我的一个片段中,我有一个 ListView ,我尝试使用ArrayAdapter进行设置,并从data.xml接收信息。

发生错误的片段类:

public static class SongsFragment extends ListFragment  {


        private static final String ARG_SECTION_NUMBER = "section_number";




        public SongsFragment() {
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public void onActivityCreated(Bundle b){
            super.onActivityCreated(b);
            //AlbumAdapt loadAlbums = new AlbumAdapt(this.getView(), this.getActivity());


            ListView listView = (ListView) getView().findViewById(R.layout.fragment_item_list);
            List<AlbumModel> albums = null;

            try {
                XMLData parser = new XMLData();
                albums = parser.parse(getActivity().getAssets().open("data.xml"));
                ArrayAdapter<AlbumModel> adapter =
                        new ArrayAdapter<AlbumModel>(getActivity(), R.layout.list_item, albums);
                listView.setAdapter(adapter);
            } catch (IOException e) {
                e.printStackTrace();
            }


        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);

            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));

        }



        @Override
        public void onDetach() {
            super.onDetach();
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_item_list, container, false);
        }

        public static SongsFragment newInstance(int sectionNumber) {
            SongsFragment fragment = new SongsFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);

            return fragment;
        }



    }

Logcat似乎很清楚:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.markbratanov.myfitplayer/com.markbratanov.myfitplayer.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.markbratanov.myfitplayer.MainActivity$SongsFragment.onActivityCreated(MainActivity.java:235)
            at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
            at android.app.Activity.performStart(Activity.java:5241)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

整个项目托管在github上,用于课堂参考等:https://github.com/markbratanov/MyFitPlayer/tree/master/Player/src/main/java/com/markbratanov/myfitplayer

任何指导/评论都会受到赞赏......我已经尝试了很多,我想要的是让这个列表视图工作,这样我就可以继续前进了。

1 个答案:

答案 0 :(得分:3)

您需要使用R.id.viewid。由于您有ListFragment

你有

 ListView listView = (ListView) getView().findViewById(R.layout.fragment_item_list);

应该是

 ListView listView = getListView();