我能够通过模拟器成功运行我的应用程序。但突然出现了一堆错误,所以现在我无法启动它。有致命错误。这是LogCat输出:
08-26 12:14:27.937 2112-2112/com.joudi.myapplication D/AndroidRuntime﹕ Shutting down VM
08-26 12:14:27.938 2112-2112/com.joudi.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.joudi.myapplication, PID: 2112
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joudi.myapplication/com.joudi.myapplication.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: 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.joudi.myapplication.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我不知道要修复“IndexOutOfBoundsException:索引0无效,大小为0”错误。这是我的MainActicity.java文件代码:
import android.graphics.Canvas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import com.joudi.myapplication.data.NoteItem;
import com.joudi.myapplication.data.NotesDataSource;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private NotesDataSource datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new NotesDataSource(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());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:2)
外观datasource.findAll();
看起来返回一个空列表。
notes.get(0);
抛出异常。
您应该检查列表的大小是否大于0。
if (notes.size() >0){
NoteItem note = notes.get(0);
note.setText("Updated!");
datasource.update(note);
}