I'm trying to convert my entire ListFragment class into a Fragment class with a ListView added to it.

Call this class HomeworkListFragment. I changed the extension from ListFragment to Fragment:

As a result I have created the following:

public class HomeworkListFragment extends Fragment{

public static final String TAG = "HomeworkListFragment";

public ListView mListView;
public HomeworkAdapter mAdapter;

private ArrayList<HomeworkObject> mCrimes;


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

    getActivity().setTitle(R.string.homework_title);
    mCrimes = HomeworkLab.get(getActivity()).getHomework();

    mAdapter = new HomeworkAdapter(mCrimes);
    mListView.setAdapter(mAdapter);

    setRetainInstance(true);

}

@TargetApi(11)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View v = super.onCreateView(inflater,parent,savedInstanceState);

    mListView = (ListView)v.findViewById(android.R.id.list);
    mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    mListView.setMultiChoiceModeListener(new MultiChoiceModeListener(){
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()){
                case R.id.menu_item_delete_homework:
                    HomeworkLab homeworkLab = HomeworkLab.get(getActivity());
                    for (int i = mAdapter.getCount() - 1;i>=0;i--){
                        //if (mListView.getListView().isItemChecked(i)){
                            //homeworkLab.deleteHomework(mAdapter.getItem(i));
                        //}
                    }
                    mode.finish();
                    mAdapter.notifyDataSetChanged();
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            //ActionMode.callback Methods
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.homework_list_item_context, menu);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode arg0) {
            //not used here
        }

        @Override
        public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
            //not used here
            return false;
        }

        @Override
        public void onItemCheckedStateChanged(ActionMode arg0,
                int arg1, long arg2, boolean arg3) {
            //not used here
        }

    });
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            HomeworkObject c = mAdapter.getItem(position);
            //Start CrimeActivity
            //Intent i = new Intent(getActivity(), CrimeActivity.class);
            Intent i = new Intent(getActivity(), HomeworkPagerActivity.class);
            i.putExtra(HomeworkFragment.EXTRA_HOMEWORK_ID, c.getId());
            startActivity(i);               
        }

    });     
    return v;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
    super.onCreateOptionsMenu(menu,inflater);
    inflater.inflate(R.menu.fragment_homework_list, menu);
}

@TargetApi(11)
@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
        case R.id.menu_item_new_homework:
            HomeworkObject homework = new HomeworkObject();
            homework.setTitle("");
            HomeworkLab.get(getActivity()).addHomework(homework);
            Intent i = new Intent(getActivity(),HomeworkPagerActivity.class);
            i.putExtra(HomeworkFragment.EXTRA_HOMEWORK_ID, homework.getId());
            startActivityForResult(i,0);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    getActivity().getMenuInflater().inflate(R.menu.homework_list_item_context, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
    int position = info.position;
    HomeworkObject h = mAdapter.getItem(position);

    switch (item.getItemId()) {
        case R.id.menu_item_delete_homework:
            HomeworkLab.get(getActivity()).deleteHomework(h);
            mAdapter.notifyDataSetChanged();
            return true;
    }
    return super.onContextItemSelected(item);
}


@Override
public void onResume() {
    super.onResume();
    mAdapter.notifyDataSetChanged();
    mCrimes = HomeworkLab.get(getActivity()).getHomework();//update list
}

@Override
public void onPause(){
    super.onPause();
    HomeworkLab.get(getActivity()).saveHomework();
}

/*@Override
public void onListItemClick(ListView l, View v, int position, long id){
    HomeworkObject c = ((HomeworkAdapter)getListAdapter()).getItem(position);
    //Start CrimeActivity
    //Intent i = new Intent(getActivity(), CrimeActivity.class);
    Intent i = new Intent(getActivity(), HomeworkPagerActivity.class);
    i.putExtra(HomeworkFragment.EXTRA_HOMEWORK_ID, c.getId());
    startActivity(i);
}*/

private class HomeworkAdapter extends ArrayAdapter<HomeworkObject>{
    public HomeworkAdapter(ArrayList<HomeworkObject> crimes){
        super(getActivity(), 0, crimes);
    }

    @SuppressLint("SimpleDateFormat")
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        if (convertView == null){
            convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_homework, null);
        }
        HomeworkObject c = getItem(position);

        TextView titleTextView = (TextView)convertView.findViewById(R.id.homework_list_item_titleTextView);
        titleTextView.setText(c.getTitle());
        TextView dateTextView = (TextView)convertView.findViewById(R.id.homework_list_item_dateTextView);

        DateFormat df = new SimpleDateFormat("EEEE, d MMMM yyyy");     
        String reportDate = df.format(c.getDate());
        dateTextView.setText(reportDate);
        //dateTextView.setText(c.getDate().toString());

        CheckBox solvedCheckBox = (CheckBox)convertView.findViewById(R.id.homework_list_item_solvedCheckBox);
        solvedCheckBox.setChecked(c.isSolved());

        return convertView;
    }
}

}

My main issue is that this keeps crashing for some reason. Here's my error log:

07-19 16:42:54.323: E/AndroidRuntime(8188): FATAL EXCEPTION: main 07-19 16:42:54.323: E/AndroidRuntime(8188): Process: com.WaylonHuang.android.homework, PID: 8188 07-19 16:42:54.323: E/AndroidRuntime(8188): java.lang.NullPointerException 07-19 16:42:54.323: E/AndroidRuntime(8188): at com.WaylonHuang.android.homework.HomeworkListFragment.onCreateView(HomeworkListFragment.java:59) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.support.v4.view.ViewPager.populate(ViewPager.java:914) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.View.measure(View.java:16537) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.View.measure(View.java:16537) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.View.measure(View.java:16537) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.View.measure(View.java:16537) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.View.measure(View.java:16537) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.View.measure(View.java:16537) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.View.measure(View.java:16537) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135) 07-19 16:42:54.323: E/AndroidRuntime(8188): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.View.measure(View.java:16537) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 07-19 16:42:54.323: E/AndroidRuntime(8188): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.View.measure(View.java:16537) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1947) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1137) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1326) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5796) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.Choreographer.doCallbacks(Choreographer.java:574) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.Choreographer.doFrame(Choreographer.java:544) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.os.Handler.handleCallback(Handler.java:733) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.os.Handler.dispatchMessage(Handler.java:95) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.os.Looper.loop(Looper.java:136) 07-19 16:42:54.323: E/AndroidRuntime(8188): at android.app.ActivityThread.main(ActivityThread.java:5102) 07-19 16:42:54.323: E/AndroidRuntime(8188): at java.lang.reflect.Method.invokeNative(Native Method) 07-19 16:42:54.323: E/AndroidRuntime(8188): at java.lang.reflect.Method.invoke(Method.java:515) 07-19 16:42:54.323: E/AndroidRuntime(8188): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 07-19 16:42:54.323: E/AndroidRuntime(8188): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 07-19 16:42:54.323: E/AndroidRuntime(8188): at dalvik.system.NativeStart.main(Native Method) 07-19 16:44:33.626: D/AndroidRuntime(9087): Shutting down VM 07-19 16:44:33.626: W/dalvikvm(9087): threadid=1: thread exiting with uncaught exception (group=0x416f4d40) 07-19 16:44:33.629: E/AndroidRuntime(9087): FATAL EXCEPTION: main 07-19 16:44:33.629: E/AndroidRuntime(9087): Process: com.WaylonHuang.android.homework, PID: 9087 07-19 16:44:33.629: E/AndroidRuntime(9087): java.lang.RuntimeException: Unable to resume activity {com.WaylonHuang.android.homework/com.WaylonHuang.android.homework.HomeworkMain}: java.lang.NullPointerException 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2826) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2274) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.app.ActivityThread.access$800(ActivityThread.java:139) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.os.Handler.dispatchMessage(Handler.java:102) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.os.Looper.loop(Looper.java:136) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.app.ActivityThread.main(ActivityThread.java:5102) 07-19 16:44:33.629: E/AndroidRuntime(9087): at java.lang.reflect.Method.invokeNative(Native Method) 07-19 16:44:33.629: E/AndroidRuntime(9087): at java.lang.reflect.Method.invoke(Method.java:515) 07-19 16:44:33.629: E/AndroidRuntime(9087): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 07-19 16:44:33.629: E/AndroidRuntime(9087): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 07-19 16:44:33.629: E/AndroidRuntime(9087): at dalvik.system.NativeStart.main(Native Method) 07-19 16:44:33.629: E/AndroidRuntime(9087): Caused by: java.lang.NullPointerException 07-19 16:44:33.629: E/AndroidRuntime(9087): at com.WaylonHuang.android.homework.HomeworkListFragment.onCreate(HomeworkListFragment.java:48) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.support.v4.app.Fragment.performCreate(Fragment.java:1477) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.support.v4.app.FragmentActivity.onResume(FragmentActivity.java:445) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1215) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.app.Activity.performResume(Activity.java:5327) 07-19 16:44:33.629: E/AndroidRuntime(9087): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2811) 07-19 16:44:33.629: E/AndroidRuntime(9087): ... 12 more

As far as I can tell there's an error in the onCreateView method with some null pointer error but I can't make heads or tails of this issue.

I recently picked up Android after moving from iOS so I'm pretty new to this. Any explanation could help a lot!

Related posts

Recent Viewed