Android 错误:请求索引0,大小为0。我该如何解决?
这是我的代码:Android 错误:请求索引0,大小为0。我该如何解决?,android,cursor,Android,Cursor,这是我的代码: package com.dev.paolo.sicinf; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.ListView; import java.ma
package com.dev.paolo.sicinf;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ListView;
import java.math.BigInteger;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
public class KeySettings extends ActionBarActivity {
public void generateKey(View view)
{
Intent RSA = new Intent (this, RSA.class);
startActivity(RSA);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_key_settings);
ListView listView = (ListView)findViewById(R.id.list_keys);
List list = new LinkedList();
ManagerDB db = new ManagerDB(this);
Cursor c= null;
String textD,textE,textN;
BigInteger d,e,N;
try {
db.open();
} catch (SQLException a) {
a.printStackTrace();
}
try
{
c = db.getKey(0);
}
catch (SQLException a)
{
a.printStackTrace();
}
textD = c.getString(2);
d = new BigInteger(textD);
try
{
c = db.getKey(1);
}
catch (SQLException a) {
a.printStackTrace();
}
textE = c.getString(2);
e = new BigInteger(textE);
try
{
c = db.getKey(2);
} catch (SQLException a)
{
a.printStackTrace();
}
textN = c.getString(2);
N = new BigInteger(textN);
db.close();
list.add(new Key("Public Key",e + " , " + N));
list.add(new Key("Private Key",d + " , " + N));
CustomAdapter adapter = new CustomAdapter(this,R.layout.rowcustom,list);
listView.setAdapter(adapter);
}
}
这是报告的错误:
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:164)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5350)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
我不知道如何解决这个问题。我尝试了许多在线指南,但没有找到解决方案。发生此错误的原因是
db.getKey()
正在尝试访问空列表中的第一项(在这种情况下,我猜是一个键列表)。在到达此点之前,您需要向数据库中添加一些内容,或者为空数据库添加一些处理。错误在于您的光标在哪个方法/行中为空。Der Golem,您能告诉我如何解决此问题吗?关于“为空数据库添加一些处理”,我如何才能做到这一点?如果你能告诉我要写的确切代码,那就更好了。如果你有权访问db.keys
列表(假设这就是它的名称,我从未使用过ManagerDB),那么添加一个检查if(db.keys.size>0)
否则,将CursorIndexOutOfBoundsException
添加到try/catch语句的catch块中,并处理其中的错误抱歉,但我无法理解。你能确切地告诉我应该如何修改代码吗?