Android:Listview SetOnClickListener导致NullPointerException

时间:2014-12-15 22:06:45

标签: android listview

我的onCreate方法中有这个代码,当调用onCreateMethod时会导致NullPointerException。

lvCheckList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // TODO Auto-generated method stub
    }
});
}

LogCat:http://pastebin.com/0bU90xbh

我查看了其他用户的类似帖子,但找不到有效的答案。

编辑:onCreate方法:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.checklist);
    Button bAddItem = (Button) findViewById(R.id.bAddItem);
    final EditText etItemName = (EditText) findViewById(R.id.etItemName);
    final TextView tvTitle = (TextView) findViewById(R.id.tvTitle);

    //----------------lvCheckList is defined here -------------------
    ListView lvCheckList = (ListView) findViewById(R.id.lvCheckLists);
    // lvCheckList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    // lvCheckList.setTextFilterEnabled(true);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String selectedItem = extras.getString("selectedItem");
        // get the value based on the key
        currentList = selectedItem;
    }
    tvTitle.setText(currentList);
    checkListData = getSharedPreferences(filename, 0);
    updateListView();

    //------------------- OnItemClickListener -----------------------------
    lvCheckList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub

                }

            });

1 个答案:

答案 0 :(得分:0)

这意味着您的lvCheckList为空。确保为listview指定了一个值:

lvChecklist = (ListView) findViewById(R.id.your_listview);