错误:请求索引0,大小为0.我该如何解决?

时间:2015-04-29 11:43:03

标签: 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.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

我不知道如何解决这个问题。我在网上尝试了很多指南,但我还没有找到解决方案。

1 个答案:

答案 0 :(得分:0)

发生此错误是因为db.getKey()正在尝试访问空列表中的第一项(在这种情况下我猜测键列表)。您需要在到达此点之前向数据库添加内容或为空数据库添加一些处理。