2015-10-19 105 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(); 
}