Appliction throw java.lang.IndexOutOfBoundsException:索引1无效,删除项后大小为0

Appliction throw java.lang.IndexOutOfBoundsException:索引1无效,删除项后大小为0,java,android,android-fragments,Java,Android,Android Fragments,我的应用程序抛出java.lang.IndexOutOfBoundsException:索引1无效,删除项后大小为0 提供以下内容的片段代码: package jvs.com.notes.fragments; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android

我的应用程序抛出java.lang.IndexOutOfBoundsException:索引1无效,删除项后大小为0
提供以下内容的片段代码:

 package jvs.com.notes.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import jvs.com.notes.R;

import static jvs.com.notes.R.id.etNewItem;

/**
 * A simple {@link Fragment} subclass.
 */
public class NoteFragment extends Fragment {
private ArrayList<String>items;
    private ArrayAdapter<String> itemsAdapter;
    private ListView lvItems;
    //we create this variable for using with different elements
    private View root;
    private Bundle savedState = null;

    public NoteFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        root=inflater.inflate(R.layout.fragment_note,container,false);
        initView();
        readItems();
        //setup remove listener method
        setupListViewListener();

        return root;
    }
    //Initializing of view
private void initView(){
    lvItems =(ListView) root.findViewById(R.id.lvItems);
    items = new ArrayList<String>();
    //creating adapter for viewing list of notes
    itemsAdapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1,items);
    lvItems.setAdapter(itemsAdapter);
  //  items.add("First Item");
 //   items.add("Second Item");
    //initializing button add Note
    Button b =(Button) root.findViewById(R.id.btnAddItem);
    b.setOnClickListener(mButtonClickListener);

}
//When we use fragment we have to use onClickListener
private View.OnClickListener mButtonClickListener=new View.OnClickListener(){
    public void onClick(View v){
        onAddItem();
    }
};
//method for  adding Items
public  void onAddItem(){
    //initialisation of view for adding notes
    EditText etNewItem =(EditText)root.findViewById(R.id.etNewItem);
    String itemText = etNewItem.getText().toString();
    itemsAdapter.add(itemText);
    etNewItem.setText(" ");
    writeItems();
}

//When the item is long clicked it removes item
private void setupListViewListener(){
    //attaching a long click listener to the listview
    lvItems.setOnItemLongClickListener(
            new AdapterView.OnItemLongClickListener(){
                @Override
                public boolean onItemLongClick(AdapterView<?> adapter,
                                               View item,int pos,long id){
                  items.remove(pos);
                    //Refresh the adapter
                    itemsAdapter.notifyDataSetChanged();
                    writeItems();
                    //Return true consumes the long click event(marks it handled)
                    return true;
                }
            }
    );
}
//method for writing and reading notes from file
    private void readItems(){
        File filesDir = getActivity().getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {
            items = new ArrayList<String>(FileUtils.readLines(todoFile));
        }catch (IOException e){
            items =new ArrayList<String>();
        }
    }
    private void writeItems() {
        File filesDir = getActivity().getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {
            FileUtils.writeLines(todoFile, items);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

91行属于项目。删除(pos)

假设列表中有要删除的元素,异常显示在您尝试删除索引
1
中的元素时包含0个元素的集合

Invalid index 1, size is 0
       at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
       at java.util.ArrayList.remove(ArrayList.java:403)
这里可能发生了一个异常,由于您只需将集合实例化为一个新的空集合,因此在尝试删除时会出现异常

 private void readItems(){
        File filesDir = getActivity().getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {
            items = new ArrayList<String>(FileUtils.readLines(todoFile));
        }catch (IOException e){
            //Write some logs here... Exception probably happens here
            //But you will never know since you are ignoring it.
            items =new ArrayList<String>();
        }
    }

指出这个问题非常困难,因为你所有的物品都有类似的名称,试着为你的物品使用不同的名称,这只是一个建议,让我们直截了当地说:

问题在您的代码中:

 //method for  adding Items
public  void onAddItem(){
//initialisation of view for adding notes
EditText etNewItem =(EditText)root.findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
itemsAdapter.add(itemText); // Here is the issue
etNewItem.setText(" ");
writeItems();
}
问题在于,您正在“items”arraylist中添加项目,而不是在“items”arraylist中添加项目,但在删除项目时,您正试图将其从“items”arraylist中删除。甚至不存在的地方。将上面给出的方法替换为以下代码:

//method for  adding Items
public  void onAddItem(){
//initialisation of view for adding notes
EditText etNewItem =(EditText)root.findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
items.add(itemText);
itemsAdapter.notifyDataSetChanged();
etNewItem.setText(" ");
writeItems();
}

希望它能起作用

您不需要在删除位置后放入notifyDataSetChanged(),而是在代码中使用notifyItemRemoved(位置)或notifyItemRangeRemoved(位置),然后重试

您有办法知道数组中有什么吗?现在它赢了“t删除该项目,日志中没有任何事件我有这个06-27 14:18:49.257 816-827/?E/DatabaseUtils:将异常写入包java.lang.SecurityException:权限拒绝:获取/设置用户请求以用户-2身份运行但正在从用户0调用的设置;这需要在com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:14643)的android.app.ActivityManager.handleIncomingUser上完整地跨用户交互(ActivityManager.java:2469)@Сааааааааааааааааааааааааааааood。我在删除项目时遇到问题。应用程序在长时间单击项目后关闭。这是因为在删除项目时,它找不到任何要删除的内容。主要问题是AddItem导致删除项目失败。只需替换此代码,一切都会正常工作。仍然关闭是的,仍然是相同的错误,在尝试添加n时我也有错误ew注意到一定有什么地方丢失了。你能给我你的完整代码吗,这样我就可以调试它,看看真正的问题在哪里?
 //method for  adding Items
public  void onAddItem(){
//initialisation of view for adding notes
EditText etNewItem =(EditText)root.findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
itemsAdapter.add(itemText); // Here is the issue
etNewItem.setText(" ");
writeItems();
}
//method for  adding Items
public  void onAddItem(){
//initialisation of view for adding notes
EditText etNewItem =(EditText)root.findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
items.add(itemText);
itemsAdapter.notifyDataSetChanged();
etNewItem.setText(" ");
writeItems();
}