0

所以我正在尝试为我正在制作的另一个应用程序制作一个基本的前景。
我只是想让一个 listView 在我的片段中的屏幕上填充。(创建可滑动的标签视图)

我在这里的这一行不断收到 nullPointerException:

List.setAdapter(Adapter);

这是我的整个班级:

public class List_View extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.listviewtest, container, false);
    ListView List = (ListView) getActivity().findViewById(R.id.listView1); 

    List<HashMap<String, String>> Data = new ArrayList<HashMap<String, String>>();
    {
        HashMap<String, String> temp1 = new HashMap<String, String>();
        temp1.put("Item", "Item 1");
        Data.add(temp1);
        HashMap<String, String> temp2 = new HashMap<String, String>();
        temp2.put("Item", "Item 2");
        Data.add(temp2);
        HashMap<String, String> temp3 = new HashMap<String, String>();
        temp3.put("Item", "Item 3");
        Data.add(temp3);
    }

    SimpleAdapter Adapter = new SimpleAdapter(this.getActivity(),
            Data, R.layout.custom_row_view, new String[] {
                    "Item" }, new int[] { R.id.text1});

    List.setAdapter(Adapter);

    List.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            switch (position) {

            case 1:
                AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
                        getActivity());
                dlgAlert.setMessage("Set Message Text");
                dlgAlert.setTitle("Set Message Title");
                dlgAlert.setNeutralButton("View info",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {

                                dialog.dismiss();
                            }
                        });

                dlgAlert.setCancelable(false);
                dlgAlert.create().show();

                break;
            }
        }
    });

    return rootView;
}
}

据我所知,这不应该返回 null ..

4

1 回答 1

2

我在想你的膨胀操作返回空值。检查这一行

ListView List = (ListView) getActivity().findViewById(R.id.listView1);  

您应该从片段的父视图中膨胀您的 ListView,而不是从父活动中。

应该是这样的

ListView List = (ListView) rootView.findViewById(R.id.listView1);  and make sure that
于 2013-10-21T02:28:06.383 回答