0

关于光标的使用真的很困惑。

我有一项工作正常的活动,代码如下:

public class AreaActivity extends ListActivity {

private TextView secondaryTitle;
private Button newArea;
private static final int ACTIVITY_CREATE=0;

private RMDbAdapter rmDbHelper;
private AlertDialog clickOptionsDialog;
private long inspectionId;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_area);
    rmDbHelper = new RMDbAdapter(this);
    rmDbHelper.open();
    Intent i = getIntent();
    inspectionId = i.getLongExtra("Intent_InspectionID", -1);
    setUpViews();
    setLongClick();

    // Get a Cursor for the list items
    Cursor listCursor = rmDbHelper.fetchAllAreasForInspection(inspectionId);
    startManagingCursor(listCursor);           

    // set the custom list adapter     
    setListAdapter(new MyListAdapter(this, listCursor));



}

private void setUpViews() {
    secondaryTitle = (TextView)findViewById(R.id.secondary_title);
    final Cursor cursor = (Cursor) rmDbHelper.fetchInspection(inspectionId);
    String inspectionRef = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex( 
            RMDbAdapter.INSPECTION_REF)), "Reference unknown"); 
    String companyName = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex( 
            RMDbAdapter.INSPECTION_COMPANY)), "company unknown"); 
    cursor.close();
    final String secondaryTitleText = inspectionRef + ", " + companyName;
    secondaryTitle.setText(secondaryTitleText);
    newArea = (Button)findViewById(R.id.new_area);
    newArea.setOnClickListener(new View.OnClickListener() {                     
        public void onClick(View v) {
            createArea();
        }
    }); 

}

光标从数据库中获取信息,然后检查它是否为空(使用本网站上向我推荐的单独类中的代码)并将文本设置在TextView.

但是,当我在下一个活动中使用这个确切的代码来做同样的事情时,我得到了错误:

CursorIndexOutOfBoundsException: 请求索引 0,大小为 0..

代码是一样的,所以我猜这是复制游标或不使用的问题,moveToFirst但我所做的似乎没有解决问题。

4

1 回答 1

0

由于 njzk2 发现了问题 - 当我将inspectionId 传递给 Intent 时出现愚蠢的错误..

于 2012-09-17T16:09:28.457 回答