在Android中的片段中显示数据

时间:2015-10-01 02:33:39

标签: android android-fragments android-sqlite

我正在尝试显示来自SQLite的数据并将其显示在fragmentview的listview中。这是我的代码,当我运行它时,它会自动关闭。此代码位于我的主要活动

    public static class ListDoctorFragment extends Fragment {

    ListView list;
    DataDB data = new DataDB();

    ArrayAdapter<String> listAdapter;

    public ListDoctorFragment() {
    }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            final View view = inflater.inflate(R.layout.listdoctor, container, false);

            ArrayList<String> names = new ArrayList<String>();
            try {
                names = data.getDoctorlistDB(getActivity());
            } catch (SQLException e) {
                e.printStackTrace();
            }

            listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.support_simple_spinner_dropdown_item, names);

            // set the adapter
            list.setAdapter(listAdapter);

            return view;
        }
    }

1 个答案:

答案 0 :(得分:2)

您的列表视图列表&#39;未初始化。它必须是这样的:

list = (ListView) view.findViewById(R.id.listview);