我正在尝试将我的整个ListFragment类转换为具有ListView的Fragment类。

将该类称为HomeworkListFragment。我将扩展名从ListFragment更改为Fragment:

结果,我创建了以下内容:

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;
    }
}


}

据我所知,onCreateView方法中存在一个错误,并带有一些空指针错误,但我无法对此问题做出正面或反面的解释。

我最近从iOS移出后选择了Android,因此我对此很陌生。任何解释都可以帮助很多!

最佳答案

问题:

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


android.R.id.list仅绑定到ListFragment,而不在Fragment中,这就是为什么它为null的原因。

作为ListFragment的documentation

ListFragment has a default layout that consists of a single list view.
However, if you desire, you can customize the fragment layout by returning
your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle).
To do this, your view hierarchy must contain a ListView object with the
id "@android:id/list" (or list if it's in code)


解:

您需要在片段中添加一个视图,并使用ListView创建布局,并使用创建的ListView ID代替android.R.id.list

关于java - 将ListView添加到具有崩溃的 fragment ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24845720/

10-11 20:09