IndexOutOfBound和Null对象参考

时间:2016-01-31 14:23:39

标签: android indexoutofboundsexception

我正在制作todo应用。我正在经历展示活动时遇到这两个错误:

IndexOutOfBoundsException

NullObjectReference

这是我的班级:

public class ShowToDoActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> {

private SimpleCursorAdapter adapter;

private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;
private static final int DELETE_ID = Menu.FIRST + 1;


DbContentProvider dbcp;
SQLiteDatabase database;
DBCompanyClass db;
private String[] allTodoColumns = {DBCompanyClass.COL_TODO_ID, DBCompanyClass.COL_TODO_NAME, DBCompanyClass.COL_TODO_CATEGORY};

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

    fillData();
    registerForContextMenu(getListView());

}

// create the menu based on the XML defintion
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.listmenu, menu);
    return true;
}

// Reaction to the menu selection
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.insert:
            createTodo();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

private void createTodo() {
    Intent i = new Intent(this, AddTodoActivity.class);
    startActivity(i);
}

private void fillData() {

    String[] from = new String[]  {DBCompanyClass.COL_TODO_NAME};
    ArrayList<String> from_updated=new ArrayList<String>();
    String[] array = new String[from.length];

    this.db = new DBCompanyClass(this);
    database = db.getWritableDatabase();
    Cursor from_cursor = database.query(DBCompanyClass.TABLE_NAME_TODO,allTodoColumns,null,null,null,null,DBCompanyClass.COL_TODO_CATEGORY) ;

    from_cursor.moveToFirst();


    while(!from_cursor.isAfterLast()){
        from_updated.add(from_cursor.getInt(0),from_cursor.getString(1));
        from_cursor.moveToNext();
    }

    array = from_updated.toArray(array);


    // Fields on the UI to which we map
    int[] to = new int[] { R.id.label };

    getLoaderManager().initLoader(0, null, this);

    adapter = new SimpleCursorAdapter(this,R.layout.datarow,null,array,to,0);

    setListAdapter(adapter);

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    getListView().getAdapter();

    // You might consider using Bundle.putStringArray() instead
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case DELETE_ID:
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                    .getMenuInfo();
            Uri uri = Uri.parse(DbContentProvider.CONTENT_URI + "/"
                    + info.id);
            getContentResolver().delete(uri, null, null);
            fillData();
            return true;
    }
    return super.onContextItemSelected(item);
}



// Opens the second activity if an entry is clicked
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent edit = new Intent(this, AddTodoActivity.class);
    Uri todoUri = Uri.parse(DbContentProvider.CONTENT_URI + "/" + id);
    edit.putExtra(DbContentProvider.CONTENT_ITEM_TYPE, todoUri);
    startActivity(edit);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = { DBCompanyClass.COL_TODO_ID , DBCompanyClass.COL_TODO_NAME};
    CursorLoader cursorLoader = new CursorLoader(this,
            DbContentProvider.CONTENT_URI, projection, null, null, null);
    return cursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data)
{
    adapter.swapCursor(data);
}

// data is not available anymore, delete reference
@Override
public void onLoaderReset(Loader<Cursor> loader)
{
    adapter.swapCursor(null);
}

}

这是我在运行App时遇到错误的类。我也尝试调试它。

我收到的是IndexOutOfBoundsException: from_updated.add(from_cursor.getInt(0),from_cursor.getString(1));

你能告诉我为什么会这样吗?

0 个答案:

没有答案