Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android IndexOutOfBoundsException:索引0无效,大小为0_Java_Android_Listview - Fatal编程技术网

Java Android IndexOutOfBoundsException:索引0无效,大小为0

Java Android IndexOutOfBoundsException:索引0无效,大小为0,java,android,listview,Java,Android,Listview,是的,我相信有很多关于这个错误的问题,但是我相信它与以下内容有关,如下所示 我有3个列表视图mNote1、mNote2、mNote3,它们都通过registerForContextMenu()传递。然后是onCreateContextMenu方法: 请注意,在课堂活动的顶部有,private static final int MENU\u DELETE\u ID=1004;私有int-currentNoteId public void onCreateContextMenu(ContextMen

是的,我相信有很多关于这个错误的问题,但是我相信它与以下内容有关,如下所示

我有3个列表视图
mNote1、mNote2、mNote3
,它们都通过
registerForContextMenu()传递。然后是
onCreateContextMenu
方法:

请注意,在课堂活动的顶部有,
private static final int MENU\u DELETE\u ID=1004;私有int-currentNoteId

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        currentNoteId = (int) info.id;

        menu.add(0, MENU_DELETE_ID, 0, "Delete");

    }
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        currentNoteId = (int) info.id;

        menu.add(0, MENU_DELETE_ID, 0, "Delete");

    }
然后选择
onContextItemSelected
方法:

@Override
    public boolean onContextItemSelected(MenuItem item) {

        if(item.getItemId() == MENU_DELETE_ID){
            NoteItem note = noteList.get(currentNoteId);
            NoteItem note1 = noteList1.get(currentNoteId);
            NoteItem note2 = noteList2.get(currentNoteId);
            datasource.remove(note);
            datasource1.remove(note1);
            datasource2.remove(note2);
            refreshDisplay(1001);
            refreshDisplay(1002);
            refreshDisplay(1003);

        }

        return super.onContextItemSelected(item);
    }
@Override
    public boolean onContextItemSelected(MenuItem item) {

        if(item.getItemId() == MENU_DELETE_ID){
            NoteItem note = noteList.get(currentNoteId);
            NoteItem note1 = noteList1.get(currentNoteId);
            NoteItem note2 = noteList2.get(currentNoteId);
            datasource.remove(note);
            datasource1.remove(note1);
            datasource2.remove(note2);
            refreshDisplay(1001);
            refreshDisplay(1002);
            refreshDisplay(1003);

        }

        return super.onContextItemSelected(item);
    }
我相信,当ListView上的注释被按下时,它们会创建“Delete”选项,然后当选择Delete选项时,注释会被删除。我相信
onContextItemSelected
方法就是错误所在

在应用程序上,我在第一个ListView中创建了一个便笺,由于应用程序崩溃,我无法删除它。我可以在多个列表视图中创建多个注释,但无法删除它们

NoteDataSource文件如下所示,这将创建datasource1、datasource2等

public class NoteDataSource {

    private SharedPreferences notePrefs;

    public NoteDataSource(Context context, String PREFKEY){
        notePrefs = context.getSharedPreferences(PREFKEY, Context.MODE_PRIVATE);
    }

    public List<NoteItem> findAll(){

        Map<String, ?> noteMap = notePrefs.getAll();

        SortedSet<String> keys = new TreeSet<String>(noteMap.keySet());

        List<NoteItem> noteList = new ArrayList<NoteItem>();

        for (String key:keys) {
            NoteItem note = new NoteItem();
            note.setKey(key);
            note.setText((String) noteMap.get(key));
            noteList.add(note);
        }

        return noteList;

    }

    public boolean remove(NoteItem item){

        if (notePrefs.contains(item.getKey())){
            SharedPreferences.Editor editor = notePrefs.edit();
            editor.remove(item.getKey());
            editor.apply();
        }

        return true;
    }
    public boolean update(MealItem item){

        SharedPreferences.Editor editor = mealPrefs.edit();
        editor.putString(item.getKey(), item.getText());
        editor.apply();

        return true;
    }

}
notesList
初始化在类的顶部,
List notesList共有3个,notesList、notesList 1和notesList 2

我不确定这个错误是从哪里来的,但我知道当我试图删除一个注释时,应用程序崩溃了,我做的注释仍然存在

错误日志显示

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
                                                                            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                            at java.util.ArrayList.get(ArrayList.java:308)
                                                                            at com.example.notesapp.mainact.onContextItemSelected(MainAct.java:166)
                                                                            at android.app.Activity.onMenuItemSelected(Activity.java:2928)
我想我应该做的是确定我选择的note listview在哪个数据源中,因为目前我正在像上面的
onContextItemSelected
中所示那样删除它们:

NoteItem note = noteList.get(currentNoteId);
                NoteItem note1 = noteList1.get(currentNoteId);
                NoteItem note2 = noteList2.get(currentNoteId);
                datasource.remove(note);
                datasource1.remove(note1);
                datasource2.remove(note2);
                refreshDisplay(1001);
                refreshDisplay(1002);
                refreshDisplay(1003);
任何帮助都将不胜感激。谢谢大家!

编辑:

我一直在关注Lynda.com为Android构建笔记应用程序的教程(2013年)。我已经创建了我的应用程序,因此它有3个列表视图,而不是一个。用户可以根据他们按下的按钮向列表视图添加注释。因此,如果他们按下按钮1,然后输入文本并回击,它将出现在listview1上,等等

如果我有一个ListView,我的程序会工作,因为我只能从第一个ListView中删除注释。第一个列表视图分别对应于
数据源
注释列表
,而第二个和第三个列表视图分别对应于
数据源1
数据源2
注释列表1
注释列表2

要删除注释,请按住ListView注释,然后单击删除。这适用于第一个ListView,但不适用于第二个和第三个,应用程序崩溃。错误日志显示。以下方法是我在按住delete按钮时创建该按钮以及删除该按钮时使用的方法

请注意,在课堂活动的顶部有,
private static final int MENU\u DELETE\u ID=1004;私有int-currentNoteId

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        currentNoteId = (int) info.id;

        menu.add(0, MENU_DELETE_ID, 0, "Delete");

    }
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        currentNoteId = (int) info.id;

        menu.add(0, MENU_DELETE_ID, 0, "Delete");

    }
然后选择
onContextItemSelected
方法:

@Override
    public boolean onContextItemSelected(MenuItem item) {

        if(item.getItemId() == MENU_DELETE_ID){
            NoteItem note = noteList.get(currentNoteId);
            NoteItem note1 = noteList1.get(currentNoteId);
            NoteItem note2 = noteList2.get(currentNoteId);
            datasource.remove(note);
            datasource1.remove(note1);
            datasource2.remove(note2);
            refreshDisplay(1001);
            refreshDisplay(1002);
            refreshDisplay(1003);

        }

        return super.onContextItemSelected(item);
    }
@Override
    public boolean onContextItemSelected(MenuItem item) {

        if(item.getItemId() == MENU_DELETE_ID){
            NoteItem note = noteList.get(currentNoteId);
            NoteItem note1 = noteList1.get(currentNoteId);
            NoteItem note2 = noteList2.get(currentNoteId);
            datasource.remove(note);
            datasource1.remove(note1);
            datasource2.remove(note2);
            refreshDisplay(1001);
            refreshDisplay(1002);
            refreshDisplay(1003);

        }

        return super.onContextItemSelected(item);
    }
应用程序在NoteItem note1=noteList1.get(currentNoteId)时崩溃;我不知道从这里该往哪里走

编辑2:

好的,当我选择删除而不是删除它时,我烤了一下
currentNoteId
。第一个音符是0,第二个音符是1,以此类推。所以我相信当我调用notesList.get(currentNoteId)时,就是从该数组中获取索引


我想我需要某种方式来表示我从哪个ListView中选择了它,它需要位于我问题最顶端的
onCreateContextMenu
中。

在我的
onCreateContextMenu
方法中,我添加了以下行:
viewNoteId=v.getId()获取创建上下文菜单的ListView的id

然后,我在
onContextItemSelected
方法中添加了一个switch case元素,如下所示:

    if (item.getItemId() == MENU_DELETE_ID){
        switch (viewNoteId){
            case R.id.listMonB:
                NoteItem note = notesList.get(currentNoteId);
                datasource.remove(note);
                refreshDisplay(1001);
                break;
            case R.id.listMonL:
                NoteItem note1 = notesList1.get(currentNoteId);
                datasource1.remove(note1);
                refreshDisplay(1002);
                break;
            case R.id.listMonD:
                NoteItem note2 = notesList2.get(currentNoteId);
                datasource2.remove(note2);
                refreshDisplay(1003);
                break;

        }
    }

它现在起作用了

哪一行导致错误?我不确定,但它在方法
onContextItemSelected
中。我已经概述了我认为我应该在我的问题中做些什么。最好从错误发生的确切位置开始。您发布的stacktrace告诉您com.example.notesapp.mainact.onContextItemSelected(mainact.java:166)
。因为你还没有发布整个文件,即使你发布了,我也不能数到166,你需要指出这是哪一行。这是我第一个使用Android和Java的合适项目,所以我不确定该怎么做?你不确定哪一部分?