SQLite和android

SQLite和android,android,sqlite,Android,Sqlite,我是android新手,我正在创建一个存储学生信息的应用程序。我无法使用SQLITE验证登录。当我运行下面的代码时,它会将我引导到登录页面,但我会收到错误“不幸的是,yourapp已停止” 下面是DBAdapter.java public class DBAdapter { private static final String DATABASE_NAME = "Student_Info"; private static final int DATABASE_VE

我是android新手,我正在创建一个存储学生信息的应用程序。我无法使用SQLITE验证登录。当我运行下面的代码时,它会将我引导到登录页面,但我会收到错误“不幸的是,yourapp已停止”

下面是DBAdapter.java

public class DBAdapter {
        private static final String DATABASE_NAME = "Student_Info";
        private static final int DATABASE_VERSION = 2;
        private static final String DATABASE_CREATE_STUDENTS = "create table Students (ID integer primary key autoincrement, "
                + "USN varchar, Password varchar);"; 
        private static final String DATABASE_SELECT_STUDENTS="students";
        public static final String Student_ID = "ID";
        public static final String Student_USN = "USN";
        public static final String Student_PASSWORD = "Password ";
        private final Context context;
        private DatabaseHelper DBHelper;
        private SQLiteDatabase db;

        public DBAdapter(Context ctx)
        {
            this.context = ctx;
            DBHelper = new DatabaseHelper(context);
        }
        private static class DatabaseHelper extends SQLiteOpenHelper
        {
            DatabaseHelper(Context context) 
            {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            public void onCreate(SQLiteDatabase db) 
            {
                System.out.println("Creating table");

                db.execSQL(DATABASE_CREATE_STUDENTS);
                //I commented this line after my initial run since    
                //I wanted to add users using SQLite Browser and the 
                //table need only be created once. 
            }

            public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
                //empty method

            }
        }   
        public DBAdapter open() throws SQLException 
        {
            db = DBHelper.getWritableDatabase();
            return this;
        }
        public void close() 
        {
            DBHelper.close();
        }
        public Cursor fetchStudent(String usn, String password) 
        {  
               Cursor myCursor = db.query(DATABASE_SELECT_STUDENTS,   
                          new String[] { Student_ID, Student_USN, Student_PASSWORD },   
                         Student_USN + "='" + usn + "' AND " +   
                         Student_PASSWORD + "='" + password + "'", null, null, null, null);  

               if (myCursor != null) {  
                    myCursor.moveToFirst();  
               }  
               return myCursor;  
          }  
        public void InsertData(String usn, String password)
        {
            String sql="INSERT INTO students (USN,Password) VALUES('"+usn+"','"+password+"')";
            db.execSQL(sql);
        }


}


This Is The Error shown by logcat

04-18 02:04:33.010: W/dalvikvm(757): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-18 02:04:33.110: E/AndroidRuntime(757): FATAL EXCEPTION: main
04-18 02:04:33.110: E/AndroidRuntime(757): java.lang.IllegalStateException: Could not execute method of the activity
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.view.View$1.onClick(View.java:3591)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.view.View.performClick(View.java:4084)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.view.View$PerformClick.run(View.java:16966)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.os.Handler.handleCallback(Handler.java:615)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.os.Looper.loop(Looper.java:137)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-18 02:04:33.110: E/AndroidRuntime(757):  at java.lang.reflect.Method.invokeNative(Native Method)
04-18 02:04:33.110: E/AndroidRuntime(757):  at java.lang.reflect.Method.invoke(Method.java:511)
04-18 02:04:33.110: E/AndroidRuntime(757):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-18 02:04:33.110: E/AndroidRuntime(757):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-18 02:04:33.110: E/AndroidRuntime(757):  at dalvik.system.NativeStart.main(Native Method)
04-18 02:04:33.110: E/AndroidRuntime(757): Caused by: java.lang.reflect.InvocationTargetException
04-18 02:04:33.110: E/AndroidRuntime(757):  at java.lang.reflect.Method.invokeNative(Native Method)
04-18 02:04:33.110: E/AndroidRuntime(757):  at java.lang.reflect.Method.invoke(Method.java:511)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.view.View$1.onClick(View.java:3586)
04-18 02:04:33.110: E/AndroidRuntime(757):  ... 11 more
04-18 02:04:33.110: E/AndroidRuntime(757): Caused by: java.lang.NullPointerException
04-18 02:04:33.110: E/AndroidRuntime(757):  at com.abhineel.studentinfo.LoginActivity.loginListener(LoginActivity.java:39)
04-18 02:04:33.110: E/AndroidRuntime(757):  ... 14 more
04-18 02:04:33.250: W/ActivityManager(163):   Force finishing activity com.abhineel.studentinfo/.LoginActivity
04-18 02:04:33.310: W/WindowManager(163): Failure taking screenshot for (164x246) to layer 21015
04-18 02:04:33.850: W/ActivityManager(163): Activity pause timeout for ActivityRecord{4132cda8 com.abhineel.studentinfo/.LoginActivity}
04-18 02:04:33.110:E/AndroidRuntime(757):由以下原因引起:java.lang.NullPointerException

04-18 02:04:33.110:E/AndroidRuntime(757):在com.abhineel.studentinfo.LoginActivity.loginListener(LoginActivity.java:39)

LogCat声明在方法
loginListener


您需要查看那里。

如果发生崩溃,logcat中会出现异常stacktrace。把它包括在你的问题中。另外,请注意代码格式以使其可读。我是stackoverflow.com的新手。我正在发布logcat错误,下次发布时我一定会保持“正确格式化代码”。很抱歉给您带来不便
public class DBAdapter {
        private static final String DATABASE_NAME = "Student_Info";
        private static final int DATABASE_VERSION = 2;
        private static final String DATABASE_CREATE_STUDENTS = "create table Students (ID integer primary key autoincrement, "
                + "USN varchar, Password varchar);"; 
        private static final String DATABASE_SELECT_STUDENTS="students";
        public static final String Student_ID = "ID";
        public static final String Student_USN = "USN";
        public static final String Student_PASSWORD = "Password ";
        private final Context context;
        private DatabaseHelper DBHelper;
        private SQLiteDatabase db;

        public DBAdapter(Context ctx)
        {
            this.context = ctx;
            DBHelper = new DatabaseHelper(context);
        }
        private static class DatabaseHelper extends SQLiteOpenHelper
        {
            DatabaseHelper(Context context) 
            {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            public void onCreate(SQLiteDatabase db) 
            {
                System.out.println("Creating table");

                db.execSQL(DATABASE_CREATE_STUDENTS);
                //I commented this line after my initial run since    
                //I wanted to add users using SQLite Browser and the 
                //table need only be created once. 
            }

            public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
                //empty method

            }
        }   
        public DBAdapter open() throws SQLException 
        {
            db = DBHelper.getWritableDatabase();
            return this;
        }
        public void close() 
        {
            DBHelper.close();
        }
        public Cursor fetchStudent(String usn, String password) 
        {  
               Cursor myCursor = db.query(DATABASE_SELECT_STUDENTS,   
                          new String[] { Student_ID, Student_USN, Student_PASSWORD },   
                         Student_USN + "='" + usn + "' AND " +   
                         Student_PASSWORD + "='" + password + "'", null, null, null, null);  

               if (myCursor != null) {  
                    myCursor.moveToFirst();  
               }  
               return myCursor;  
          }  
        public void InsertData(String usn, String password)
        {
            String sql="INSERT INTO students (USN,Password) VALUES('"+usn+"','"+password+"')";
            db.execSQL(sql);
        }


}


This Is The Error shown by logcat

04-18 02:04:33.010: W/dalvikvm(757): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-18 02:04:33.110: E/AndroidRuntime(757): FATAL EXCEPTION: main
04-18 02:04:33.110: E/AndroidRuntime(757): java.lang.IllegalStateException: Could not execute method of the activity
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.view.View$1.onClick(View.java:3591)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.view.View.performClick(View.java:4084)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.view.View$PerformClick.run(View.java:16966)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.os.Handler.handleCallback(Handler.java:615)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.os.Looper.loop(Looper.java:137)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-18 02:04:33.110: E/AndroidRuntime(757):  at java.lang.reflect.Method.invokeNative(Native Method)
04-18 02:04:33.110: E/AndroidRuntime(757):  at java.lang.reflect.Method.invoke(Method.java:511)
04-18 02:04:33.110: E/AndroidRuntime(757):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-18 02:04:33.110: E/AndroidRuntime(757):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-18 02:04:33.110: E/AndroidRuntime(757):  at dalvik.system.NativeStart.main(Native Method)
04-18 02:04:33.110: E/AndroidRuntime(757): Caused by: java.lang.reflect.InvocationTargetException
04-18 02:04:33.110: E/AndroidRuntime(757):  at java.lang.reflect.Method.invokeNative(Native Method)
04-18 02:04:33.110: E/AndroidRuntime(757):  at java.lang.reflect.Method.invoke(Method.java:511)
04-18 02:04:33.110: E/AndroidRuntime(757):  at android.view.View$1.onClick(View.java:3586)
04-18 02:04:33.110: E/AndroidRuntime(757):  ... 11 more
04-18 02:04:33.110: E/AndroidRuntime(757): Caused by: java.lang.NullPointerException
04-18 02:04:33.110: E/AndroidRuntime(757):  at com.abhineel.studentinfo.LoginActivity.loginListener(LoginActivity.java:39)
04-18 02:04:33.110: E/AndroidRuntime(757):  ... 14 more
04-18 02:04:33.250: W/ActivityManager(163):   Force finishing activity com.abhineel.studentinfo/.LoginActivity
04-18 02:04:33.310: W/WindowManager(163): Failure taking screenshot for (164x246) to layer 21015
04-18 02:04:33.850: W/ActivityManager(163): Activity pause timeout for ActivityRecord{4132cda8 com.abhineel.studentinfo/.LoginActivity}