使用sqlite时,我的Android应用程序崩溃了

时间:2015-10-19 06:32:24

标签: android android-sqlite

我正在开发一个简单的Android应用程序来进行数据库操作。我正在使用sqlite数据库。我已经给出了下面的代码。当我尝试运行该应用程序时,它会崩溃。我不知道是什么原因。这是我的代码。

package com.example.devel.demofive;

import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.database.sqlite.*;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

public static SQLiteDatabase myDB;
String dbEmail;
String dbPwd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // DB creation
    myDB = openOrCreateDatabase("demoDB",MODE_PRIVATE,null);
    //Table Creation
    myDB.execSQL("create table if not exists signup(name VARCHAR,email VARCHAR,password VARCHAR);");
    //fetching data form DB
    Cursor rs = myDB.rawQuery("select * from signup", null);
    if (rs == null) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Error");
        builder.setMessage("Data not found in DB");
        builder.setPositiveButton("OK", null);
        AlertDialog dialog = builder.show();
        TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
        messageText.setGravity(Gravity.CENTER);
    } else {
        rs.moveToFirst();
        dbEmail = rs.getString(2);
        dbPwd = rs.getString(3);
        rs.close();
    }
    //getting values from the user
    final EditText emailText = (EditText) findViewById(R.id.userName);
    final String mail = emailText.getText().toString();
    final EditText pwdText = (EditText) findViewById(R.id.pwd);
    final String pwd = pwdText.getText().toString();

    Button login = (Button) findViewById(R.id.login);
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mail==dbEmail && pwd==dbPwd){
                Intent usrPage = new Intent(MainActivity.this,UserPage.class);
                startActivity(usrPage);
            }
            else {
                emailText.setText("");
                pwdText.setText("");
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Error");
                builder.setMessage("Please enter Correct Details");
                builder.setPositiveButton("OK", null);
                AlertDialog dialog = builder.show();
                TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
                messageText.setGravity(Gravity.CENTER);
            }
        }
    });

    Button signupBtn = (Button) findViewById(R.id.signup1);
    signupBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent signup = new Intent(MainActivity.this,Signup.class);
            startActivity(signup);
        }
    });
}

public static SQLiteDatabase passData()
{
    return myDB;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

这是日志详细信息

10-19 12:10:43.188 27684-27684/? I/art: Late-enabling -Xcheck:jni

10-19 12:10:43.273 27684-27684/com.example.devel.demofive V/SettingsInterface: invalidate [system]: current 1727 != cached 0

10-19 12:10:43.274 27684-27684/com.example.devel.demofive D/ActivityThread: hoder:android.app.IActivityManager$ContentProviderHolder@3d1d1e9d,provider,holder.Provider:android.content.ContentProviderProxy@2f280f12

10-19 12:10:43.278 27684-27684/com.example.devel.demofive D/Proxy: setHttpRequestCheckHandler

10-19 12:10:43.297 27684-27684/com.example.devel.demofive D/ActivityThread: BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{31bf18e0 com.example.devel.demofive}}

10-19 12:10:43.297 27684-27684/com.example.devel.demofive V/ActivityThread: Handling launch of ActivityRecord{5320799 token=android.os.BinderProxy@2df40a5e {com.example.devel.demofive/com.example.devel.demofive.MainActivity}}

10-19 12:10:43.390 27684-27684/com.example.devel.demofive V/ActivityThread: ActivityRecord{5320799 token=android.os.BinderProxy@2df40a5e {com.example.devel.demofive/com.example.devel.demofive.MainActivity}}: app=android.app.Application@6ace30c, appName=com.example.devel.demofive, pkg=com.example.devel.demofive, comp={com.example.devel.demofive/com.example.devel.demofive.MainActivity}, dir=/data/app/com.example.devel.demofive-1/base.apk

10-19 12:10:43.548 27684-27684/com.example.devel.demofive D/wangcy9: setStatusIcon occur wrong theme!

10-19 12:10:43.560 27684-27684/com.example.devel.demofive D/ColorDrawable: setColor color = -1118482, ColorDrawable = android.graphics.drawable.ColorDrawable@3c10736

10-19 12:10:43.585 27684-27684/com.example.devel.demofive D/ColorDrawable: setColor color = -14606047, ColorDrawable = android.graphics.drawable.ColorDrawable@11e5c70e

10-19 12:10:43.688 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.688 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.689 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.689 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.689 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.690 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.690 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.691 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.691 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.691 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.691 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.691 27684-27684/com.example.devel.demofive V/TextView: stopSelectionActionMode()

10-19 12:10:43.705 27684-27684/com.example.devel.demofive D/AndroidRuntime: Shutting down VM

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime: FATAL EXCEPTION: main

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime: Process: com.example.devel.demofive, PID: 27684

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.devel.demofive/com.example.devel.demofive.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2493)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:176)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:111)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:194)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5576)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:  Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:151)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:65)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at com.example.devel.demofive.MainActivity.onCreate(MainActivity.java:36)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6005)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2446)

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555) 
10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:176) 

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437) 

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:111) 

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:194) 

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5576) 

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956) 

10-19 12:10:43.705 27684-27684/com.example.devel.demofive E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751) 

10-19 12:10:45.550 27684-27684/com.example.devel.demofive I/Process: Sending signal. PID: 27684 SIG: 9

2 个答案:

答案 0 :(得分:0)

每当你处理游标时,请务必检查moveToFirst()

替换

if (rs == null)  to if (rs == null && rs.moveToFirst()) 

希望这有帮助

答案 1 :(得分:0)

实际上,浏览光标的正确方法是:

rs.moveToFirst();

while(rs.isAfterLast() == false){
   if(BuildConfig.DEBUG){
      Log.d("Cursor", "column value = " + rs.getColumnIndex(0));
   }
   rs.moveToNext();
}