2015-10-19 153 views
-5

我正在开发一个简单的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:[email protected],provider,holder.Provider:[email protected] 

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 [email protected] {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 [email protected] {com.example.devel.demofive/com.example.devel.demofive.MainActivity}}: [email protected], 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 = [email protected] 

10-19 12:10:43.585 27684-27684/com.example.devel.demofive D/ColorDrawable: setColor color = -14606047, ColorDrawable = [email protected] 

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 
+4

发表您的logcat – Mohit

+0

安置自己的logcat错误 – Asmi

+0

亚..只需一分钟 –

回答

0

当你正在处理光标,经常检查moveToFirst()

更换

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

希望这有助于

+0

你的意思是:'if(rs!= null && rs.moveToFirst())' –

0

ACTUA LLY,浏览光标的正确方法是:

rs.moveToFirst(); 

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