Java 不幸的是,我的android应用程序停止运行的原因

Java 不幸的是,我的android应用程序停止运行的原因,java,android,Java,Android,我在android中有以下代码,但当我运行它时,它会抱怨:不幸的是,它已经停止了: package com.example.trave; import java.util.List; import com.example.trave.data.NoteDataSource; import com.example.trave.data.NoteItem; import android.os.Bundle; import android.app.Activity; import android

我在android中有以下代码,但当我运行它时,它会抱怨:不幸的是,它已经停止了:

package com.example.trave;

import java.util.List;

import com.example.trave.data.NoteDataSource;
import com.example.trave.data.NoteItem;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    private NoteDataSource dataSource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dataSource = new NoteDataSource(this);
        List<NoteItem> notes = dataSource.findAll();
        NoteItem note = notes.get(0);
        note.setText("Updated!");
        dataSource.update(note);

        notes = dataSource.findAll();
        note = notes.get(0);
        Log.i("NOTES", note.getKey() + ": " + note.getText());

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

package com.example.trave.data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import android.content.Context;
import android.content.SharedPreferences;

public class NoteDataSource {

    private static final String PREFKEY = "notes";
    private SharedPreferences notePrefs;

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

    public List<NoteItem> findAll() {

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

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

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

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

    public boolean update(NoteItem note) {      
        SharedPreferences.Editor editor = notePrefs.edit();
        editor.putString(note.getKey(), note.getText());
        editor.commit();
        return true;
    }

    public boolean remove(NoteItem note) {
        if (notePrefs.contains(note.getKey())) {
            SharedPreferences.Editor editor = notePrefs.edit();
            editor.remove(note.getKey());
            editor.commit();
        }
        return true;
    }
}


package com.example.trave.data;

import android.annotation.SuppressLint;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class NoteItem {
    private String key;
    private String text;

    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }

    @SuppressLint("SimpleDateFormat") 
    public static NoteItem getNew() {

        Locale locale = new Locale("en_US");
        Locale.setDefault(locale);

        String pattern = "yyyy-MM-dd HH:mm:ss Z";
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        String key = formatter.format(new Date());

        NoteItem note = new NoteItem();
        note.setKey(key);
        note.setText("");
        return note;
    }

}

你有IndexOutOfBoundsException

List<NoteItem> notes = dataSource.findAll();
NoteItem note = notes.get(0);
因为列表似乎是空的,所以应该检查大小

List<NoteItem> notes = dataSource.findAll();
if (notes.size()>0)
    NoteItem note = notes.get(0);
List notes=dataSource.findAll();
如果(notes.size()>0)
NoteItem note=notes.get(0);

并检查您提供给列表的数据

您会得到一个
索引AutofBoundsException
,因为NoteList是空的。最有可能发生在:

    NoteItem note = notes.get(0);
onCreate
方法中。您需要检查
NoteDataSource.findAll()的空结果,如下所示:

    List<NoteItem> notes = dataSource.findAll();
    if(notes.isEmpty())
            return; // Or whatever you must do in your App
    else {
            NoteItem note = notes.get(0);
            // ... The rest of your onCreate code...
List notes=dataSource.findAll();
if(notes.isEmpty())
返回;//或者在应用程序中必须执行的任何操作
否则{
NoteItem note=notes.get(0);
//…其余的onCreate代码。。。
此外,在您的
notedasource.update()
notedasource.remove()
中,请返回
commit()
的结果,而不是从合同开始始终为true:
否则返回布尔值就没有多大意义了。

您能提供错误的logcat吗?没有我们猜测的logcat的堆栈跟踪,但是这里-
List notes=dataSource.findAll();noteim note=notes.get(0)
你不检查notes是否有任何项目。嗨,伙计,虽然logcat真的很大,我会尝试上传它。你发布了错误的异常。这是来自电子邮件应用程序。请从你的应用程序发布异常。
原因:java.lang.IndexOutofBoundException:无效索引0,大小为0 09-08 11:15:24.543:E/AndroidRuntime(1552):at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)09-08 11:15:24.543:E/AndroidRuntime(1552):at java.util.ArrayList.get(ArrayList.java:304)09-08 11:15:24.543:E/AndroidRuntime(1552):at com.example.trave.MainActivity.onCreate(MainActivity.java:25)
正如我所想,您的
注释列表是空的。
get(0)
因此将抛出越界异常。
    NoteItem note = notes.get(0);
    List<NoteItem> notes = dataSource.findAll();
    if(notes.isEmpty())
            return; // Or whatever you must do in your App
    else {
            NoteItem note = notes.get(0);
            // ... The rest of your onCreate code...