android.database.CursorIndexOutOfBoundsException:请求索引0,在Android中大小为0错误

时间:2017-01-12 21:27:05

标签: android cursor android-sqlite

我收到以下错误:

01-12 22:19:27.787 20308-20308/com.pos.example E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.pos.example, PID: 20308
                                                                         java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                             at android.view.View.performClick(View.java:5226)
                                                                             at android.view.View$PerformClick.run(View.java:21266)
                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:168)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5845)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
                                                                          Caused by: java.lang.reflect.InvocationTargetException
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                             at android.view.View.performClick(View.java:5226) 
                                                                             at android.view.View$PerformClick.run(View.java:21266) 
                                                                             at android.os.Handler.handleCallback(Handler.java:739) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                             at android.os.Looper.loop(Looper.java:168) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5845) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
                                                                          Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
                                                                             at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
                                                                             at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                                                                             at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
                                                                             at com.pos.example.LoginActivity.checkLogin(LoginActivity.java:69)
                                                                             at com.pos.example.LoginActivity.onClick(LoginActivity.java:40)
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                             at android.view.View.performClick(View.java:5226) 
                                                                             at android.view.View$PerformClick.run(View.java:21266) 
                                                                             at android.os.Handler.handleCallback(Handler.java:739) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                             at android.os.Looper.loop(Looper.java:168) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5845) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 

我的代码看起来像这样:

@Override
public void onClick(View v) {
    checkLogin = checkLogin();

    if (checkLogin == true) {
        switch (v.getId()) {
            case R.id.buttonLogin:
                intent = new Intent(this, 2.class);
                startActivity(intent);
                break;
            case R.id.textViewRegistrierung:
                intent = new Intent(this, 1.class);
                startActivity(intent);
                break;
            default:
                throw new RuntimeException("Unknown button ID");
        }
    } else {
        Toast.makeText(this, "Failed!", Toast.LENGTH_LONG).show();
    }
}

public boolean checkLogin() {
    boolean check;

    db = new DBAdapter(this);
    db.open();
    cursor = db.getRow(email.getText().toString());

    if (cursor != null) {
        String emailDB = cursor.getString(cursor.getColumnIndex("email"));
        String pwDB = cursor.getString(cursor.getColumnIndex("password"));

        if (emailDB.equals(email.getText().toString()) && pwDB.equals(passwort.getText().toString())) {
            check = true;
        } else {
            check = false;
        }
    } else {
        check = false;
    }

    cursor.close();
    db.close();
    return check;
}

你有什么想法为什么应用程序崩溃?我不知道为什么,if the user (email and password) exist in the SQLite Database it works 如果电子邮件或密码错误,应用程序崩溃 我已经尝试了几个小时修复它,但它不起作用。

1 个答案:

答案 0 :(得分:3)

即使数据库中没有记录,也要怀疑光标不为空.......

替换

if (cursor != null)

通过

if (cursor != null && cursor.getCount() != 0)