`

index -1 requested , with a size of 1

阅读更多

 

访问行如下: 

int type =  blackWhiteCursor.getInt(blackWhiteCursor.getColumnIndex(Filters.TYPE));

出现log信息:

 

05-06 10:10:06.865: ERROR/AndroidRuntime(4061): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

05-06 10:10:06.865: ERROR/AndroidRuntime(4061):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)

05-06 10:10:06.865: ERROR/AndroidRuntime(4061):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)

05-06 10:10:06.865: ERROR/AndroidRuntime(4061):     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)

05-06 10:10:06.865: ERROR/AndroidRuntime(4061):     at android.database.CursorWrapper.getInt(CursorWrapper.java:123)

05-06 10:10:06.865: ERROR/AndroidRuntime(4061):     at cn.flyfot.filter.callfilter.RuleService.blockCommonModehandler(RuleService.java:293)

05-06 10:10:06.865: ERROR/AndroidRuntime(4061):     at cn.flyfot.filter.callfilter.RuleService.blockingHandler(RuleService.java:574)

05-06 10:10:06.865: ERROR/AndroidRuntime(4061):     at cn.flyfot.filter.callfilter.PhoneStateListenerReceiver.blockingHandler(PhoneStateListenerReceiver.java:172)

05-06 10:10:06.865: ERROR/AndroidRuntime(4061):     at cn.flyfot.filter.callfilter.PhoneStateListenerReceiver.doReceiver(PhoneStateListenerReceiver.java:57)

05-06 10:10:06.865: ERROR/AndroidRuntime(4061):     at cn.flyfot.filter.callfilter.PhoneStateListenerReceiver.onReceive(PhoneStateListenerReceiver.java:39)

05-06 10:10:06.865: ERROR/AndroidRuntime(4061):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)


解决问题:
出现以上错误是由于没有移动游标或是没有查询到需要的数据而移动了游标强制获取某一行数据而导致的,注意查询条件和右边移动。

分享到:
评论
2 楼 chen299135 2012-04-14  
这个项目比较急,都愁了很久了。。。
1 楼 chen299135 2012-04-14  

package com.gallery.detail;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
* 该类完成图片的检索,显示功能
*
* @author Administrator
*
*/
public class DetailActivity extends Activity implements OnClickListener {
private ImageView photo;
private Button next = null;
private Button previous = null;
private Button message = null;
private TextView position = null;
private Cursor cursor;
private int photoIndex;
private int photoNameIndex;
private int photoIDIndex;
private int photoTitleIndex;
private int photoSizeIndex;
private String Message = null;
private int totalNum = 0;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
next = (Button) findViewById(R.id.next);
next.setOnClickListener(this);
previous = (Button) findViewById(R.id.previous);
previous.setOnClickListener(this);
message = (Button) findViewById(R.id.message);
message.setOnClickListener(this);
photo = (ImageView) this.findViewById(R.id.image_view);
position = (TextView) findViewById(R.id.number);
// 指定获取的列
String columns[] = new String[] { Media.DATA, Media._ID, Media.TITLE,
Media.DISPLAY_NAME, Media.SIZE };
// 得到一个游标

cursor = this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI,
columns, null, null, null);
// 获取指定列的索引
photoIndex = cursor.getColumnIndexOrThrow(Media.DATA);
System.out.println("photoIndex" + photoIndex);
photoNameIndex = cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);
photoIDIndex = cursor.getColumnIndexOrThrow(Media._ID);
photoTitleIndex = cursor.getColumnIndexOrThrow(Media.TITLE);
photoSizeIndex = cursor.getColumnIndexOrThrow(Media.SIZE);
// 获取图片总数
totalNum = cursor.getCount();
System.out.println("共有这么多图片"+totalNum);
System.out.println("IDyyy:" + cursor.getInt(photoNameIndex));
// 跳到第一个图片
if (cursor.moveToFirst()) {
setImage();
position.setText("(1/" + totalNum + ")");
}
}

public void onClick(View arg0) {
switch (arg0.getId()) {
// 下一个
case R.id.next:
if (cursor.moveToNext()) {
setImage();

}
break;
// 上一个
case R.id.previous:
if (cursor.moveToPrevious()) {
setImage();
}
break;
case R.id.message:
// Dialog显示详细信息
AlertDialog.Builder builder = new AlertDialog.Builder(
DetailActivity.this);
builder.setTitle("详细信息");
builder.setMessage(Message);
builder.setPositiveButton("关闭",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
break;
}
}

private void setImage() {
// 获取图片的Name
String name = cursor.getString(photoNameIndex);
// 获取图片的ID
String number = cursor.getString(photoIDIndex);
System.out.println("IDyyy:" + cursor.getInt(photoNameIndex));
// 获取图片的Title
String title = cursor.getString(photoTitleIndex);
// 获取图片的大小
String size = cursor.getString(photoSizeIndex);
// 获取图片存储路径
String path = cursor.getString(photoIndex);
// 为TextView:position赋值(现在所在的位置)
position.setText("(" + number + "/" + totalNum + ")");
Message = "Name:" + name + "\n" + "Number:" + number + "\n" + "ID:"
+ cursor.getInt(photoIDIndex) + "Title:" + title + "\n"
+ "Size:" + size + "\n" + "Path:" + path;
// 通过路径获取图片
Drawable image = Drawable.createFromPath(path);
photo.setImageDrawable(image);
}
}

04-14 20:39:50.060: ERROR/AndroidRuntime(9771): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gallery.detail/com.gallery.detail.DetailActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 39

我想查询所有图片的id,楼主有办法么?

相关推荐

    acpi控制笔记本风扇转速

    GPEs, to be used for debugging systems with a large number of ACPI interrupts. Implemented support for the "DMAR" ACPI table (DMA Redirection Table) in both the ACPICA headers and the disassembler. ...

    S7A驱动720版本

    - Support for S7-200 with CP 243-1 was added. Solved problems: - Passing of invalid OPC Item IDs caused a memory leak of the driver's global memory. After the global memory was exhausted, the ...

    计算机网络第六版答案

    b) Since each user requires 1Mbps when transmitting, if two or fewer users transmit simultaneously, a maximum of 2Mbps will be required. Since the available bandwidth of the shared link is 2Mbps, ...

    servlet2.4doc

    Overview Package Class Tree Deprecated Index Help PREV NEXT FRAMES NO FRAMES A B C D E F G H I J L P R S U V -------------------------------------------------------------------------------- A ...

    微软内部资料-SQL性能优化2

    For a process to access 3 GB of address space, the executable image must have been linked with the /LARGEADDRESSAWARE flag or modified using Imagecfg.exe. It should be pointed out that SQL Server was ...

    Dundas.Chart.for.Winform.Enterprise.v7.1.0.1812.for.VS2008

    Chart .NET: Stacked Column + 3D throws an Index was out of range exception when series have a different number of data points The accumulation distribution formula is incorrect; if open and close are ...

    数位板压力测试

    Writing simple programs should require only a few lines of code and a minimal understanding of the en-vironment. On the other hand, more advanced features and functions should be available to those ...

    2d toolkit 2.0 hotfix 2

    bug occurred in rare cases when switching from a sprite collection to another with the same named sprite in it, and the second sprite collection index was larger than the number of sprites in the ...

    Delphi7.1 Update

    * Setting MTSDataModule.AutoComplete to False in the IDE is overridden and set back to True at run time (Quality Central 4716).* Calls to ApplyUpdates(-1) on a TMTSDataModule do not work properly ...

    ViewPager 放大缩小左右移动

    * Simple implementation of the {@link OnPageChangeListener} interface with * stub implementations of each method. Extend this if you do not intend to * override every method of {@link ...

Global site tag (gtag.js) - Google Analytics