gpt4 book ai didi

android - ListView Android 中的 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 20:26:21 25 4
gpt4 key购买 nike

我有一个 Activity ,其中有一个按钮,点击按钮时应用程序崩溃问题在线 adapter.remove(list.get(i));

Logcat 详细信息在给定行 adapter.remove(list.get(i));

中提及 NullPointerException
   package com.example.veeresh.myphotogallery;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;

public class MainActivity extends ListActivity {

/** Items entered by the user is stored in this ArrayList variable */
ArrayList list = new ArrayList();

/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

/** Setting a custom layout for the list activity */
setContentView(R.layout.activity_main);

/** Reference to the add button of the layout main.xml */
Button btn = (Button) findViewById(R.id.btnAdd);

/** Reference to the delete button of the layout main.xml */
Button btnDel = (Button) findViewById(R.id.btnDel);

/** Defining the ArrayAdapter to set items to ListView */
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
adapter.add("Item 1");

/** Defining a click event listener for the button "Add" */
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.txtItem);
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
};

/** Defining a click event listener for the button "Delete" */
OnClickListener listenerDel = new OnClickListener() {
@Override
public void onClick(View v) {
/** Getting the checked items from the listview */
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();

for(int i=itemCount-1; i >= 0; i--)
{
if(checkedItemPositions.get(i)){
adapter.remove(list.get(i));
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
}
};

/** Setting the event listener for the add button */
btn.setOnClickListener(listener);

/** Setting the event listener for the delete button */
btnDel.setOnClickListener(listenerDel);

/** Setting the adapter to the ListView */
setListAdapter(adapter);
}
}

最佳答案

您的适配器包含一个列表,而您的 MainActivity 包含另一个列表。

您正在将项目添加到您的适配器,但是这不会将项目添加到您的 Mainactivity 中的列表。适配器中的列表包含“项目 1”,但 MainActivity 中的列表不包含。

OnClick 你正试图从你的 MainActivity 的列表中检索一个项目,它不包含你试图检索的项目,因此 NullpointerException

您应该从列表中添加或删除项目,然后调用 adapter.notifyDataSetChanged();

尝试:

//add an item
list.add("item 1");
adapter.notifyDataSetChanged();



//remove an item
list.remove(i);
adapter.notifyDataSetChanged();

关于android - ListView Android 中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32460279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com