繁体   English   中英

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

[英]why my android app is unfortunately has stopped

我在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

您在以下位置获得了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);

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

您得到一个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()的结果,而不是始终为true: SharedPreferences.Editor.commit,否则返回布尔值没有多大意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM