为什么我的Android应用程序不幸停止了

时间:2013-09-08 12:23:12

标签: 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.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;
    }

}

问题是什么以及如何解决?

logCat:

    09-08 11:15:23.674: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
09-08 11:15:23.793: E/Trace(1552): error opening trace file: No such file or directory (2)
09-08 11:15:24.533: D/AndroidRuntime(1552): Shutting down VM
09-08 11:15:24.533: W/dalvikvm(1552): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-08 11:15:24.543: E/AndroidRuntime(1552): FATAL EXCEPTION: main
09-08 11:15:24.543: E/AndroidRuntime(1552): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trave/com.example.trave.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
09-08 11:15:24.543: E/AndroidRuntime(1552):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at android.os.Looper.loop(Looper.java:137)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at java.lang.reflect.Method.invoke(Method.java:511)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at dalvik.system.NativeStart.main(Native Method)
09-08 11:15:24.543: E/AndroidRuntime(1552): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 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)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at android.app.Activity.performCreate(Activity.java:5104)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-08 11:15:24.543: E/AndroidRuntime(1552):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-08 11:15:24.543: E/AndroidRuntime(1552):     ... 11 more
09-08 11:15:24.552: W/ActivityManager(291):   Force finishing activity com.example.trave/.MainActivity
09-08 11:15:24.562: W/WindowManager(291): Failure taking screenshot for (246x410) to layer 21015

2 个答案:

答案 0 :(得分:1)

你在

得到了IndexOutOfBoundsException
List<NoteItem> notes = dataSource.findAll();
NoteItem note = notes.get(0);

notes = dataSource.findAll();
note = notes.get(0);

因为列表似乎是空的,你应该检查大小

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

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

答案 1 :(得分:0)

您获得IndexOutOfBoundsException,因为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...

此外,在您的NoteDataSource.update()NoteDataSource.remove()中,请返回commit()的结果,而不是合同中的SharedPreferences.Editor.commit 否则返回布尔值没什么意义。