【问题标题】:findViewById(int) on a null object reference空对象引用上的 findViewById(int)
【发布时间】:2016-03-16 20:55:28
【问题描述】:

我遇到了另一个问题。

我有一个部分有效的脚本。当我的 ListView 中的条目大约为 5-10 时,它起作用了。但是,当我在 50-60 左右再添加 50 个条目时,它会在调用脚本时抛出 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

这是 onCreate,其中 eraseButton 从 SQLite 数据库中删除列表视图中的选定行。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);


    //SQLite Database
    final DatabaseHandler db = new DatabaseHandler(this);


    //listview

    final ListView questionList = (ListView) findViewById(R.id.questionList);
    final Cursor cursor = db.getData();
    final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, new String[] { DatabaseHandler.COL_QUESTION }, new int[] { R.id.checkedTextView1 },0);
    assert questionList != null;
    questionList.setAdapter(adapter);
    questionList.setItemsCanFocus(false);
    questionList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    //Erasebutton listener
    final Button eraseButton = (Button) findViewById(R.id.eraseButton);
    assert eraseButton != null;
    eraseButton.setOnClickListener(new View.OnClickListener() { //erasebutton onclick
        public void onClick(View eraseButton) {
            SparseBooleanArray checked = questionList.getCheckedItemPositions();
            for (int i = questionList.getCount() - 1; i >= 0; i--) {
                if (checked.get(i)) {
                    //What to do with selected listitems
                    TextView tv = (TextView) questionList.getChildAt(i).findViewById(R.id.checkedTextView1);
                    db.removeData(tv.getText().toString());
                }
            }
            checked.clear();
            Cursor newCursor = db.getData();
            adapter.swapCursor(newCursor);
        }
    });


    //addbutton listener
    final Button addButton = (Button) findViewById(R.id.addButton);
    assert addButton != null;
    addButton.setOnClickListener(new View.OnClickListener() { //Nextbutton onclick
        public void onClick(View addButton) {
            EditText addText = (EditText) findViewById(R.id.addText);
            assert addText != null;
            String text = addText.getText().toString();
            if(!text.matches("") && text.startsWith("Jag har aldrig ")) {
                addText.setText("");
                db.insertData(text);
                Cursor newCursor = db.getData();
                adapter.swapCursor(newCursor);
            } else {
                Toast.makeText(Main3Activity.this, "Börja din fråga med \"Jag har aldrig\"", Toast.LENGTH_SHORT).show();
            }
        }

    });


    //Nextbutton listener
    final Button returnButton = (Button) findViewById(R.id.returnButton);
    assert returnButton != null;
    returnButton.setOnClickListener(new View.OnClickListener() { //Nextbutton onclick
        public void onClick(View returnButton) {
            Intent mainMenu = new Intent(Main3Activity.this, Main2Activity.class);
            Main3Activity.this.startActivity(mainMenu);
        }
    });


}

抛出错误的行是TextView tv = (TextView) questionList.getChildAt(i).findViewById(R.id.checkedTextView1);

有人知道为什么会发生这种情况吗?我仍然可以从列表中选择前 5-10 项并将它们删除,但是一旦我在 ListView 中选择了底部项,应用程序就会因此错误而崩溃。

【问题讨论】:

  • 在 onClickListener 不在其中之前实例化您的 TextView
  • 您应该从questionList 上设置的适配器获取文本,而不是使用getChildAt(i)
  • 请为您的适配器问题添加更多代码
  • @cricket_007 如何从适配器中的特定行获取文本?

标签: java android sqlite listview button


【解决方案1】:

如 cmets 中所述,您应该从设置在 questionList 上的适配器获取文本,而不是使用 getChildAt(i)

尝试做这样的事情

 for (int i = questionList.getCount() - 1; i >= 0; i--) {
     Cursor c = (Cursor) adapter.getItem(i);
     if (checked.get(i)) {
         // Get data from cursor
         String text = c.getString(c.getColumnIndex(DatabaseHandler.COL_QUESTION)); 
         db.removeData(text);
     }
 }

【讨论】:

  • 抱歉上次的评论,它就像一个魅力,我只是忘记添加Cursor c = (Cursor) adapter.getItem(i); 我还在你的脚本中将“光标”更改为“c”。以为你写错了。
  • 是的,我从其他地方复制的。感谢您指出这一点
猜你喜欢
  • 2016-08-05
  • 2020-10-02
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 2020-03-31
  • 2017-09-03
  • 1970-01-01
相关资源
最近更新 更多