如何修复索引1处的绑定值在android中为null

时间:2015-02-17 12:55:08

标签: android android-sqlite

我正在创建一个Android应用程序,其中包含使用sqlite数据库的注册表单和忘记密码字段后,单击忘记密码字段安全检查以显示密码与重置按钮。当我单击重置密码字段时,显示绑定索引1处的值为空请帮我修复此问题。这是我点击忘记归档后的活动代码:

 forgot_reset = (ButtonFlat)forgot_dialog.findViewById(R.id.dialog_button_Reset_forgot);
                    forgot_reset.setOnClickListener(new View.OnClickListener() 
                    {

                        @Override
                        public void onClick(View v) 
                        {
                            String stored_sec_hint = loginDataBaseAdapter.getSinlgeEntry(dialog_seq_answer);
                            if(confirm_sec_hint_forgot.equals(""))
                            {
                                Toast.makeText(Login_Page.this, "Please enter your security hint to proceed", Toast.LENGTH_LONG).show();
                            }
                            else
                            {
                                if(stored_sec_hint==null)
                                {
                                    Toast.makeText(Login_Page.this, "Please Enter correct security hint to proceed", Toast.LENGTH_LONG).show();
                                }
                                else
                                {
                                    show_pwd.setText(stored_pass);
                                }
                            }
                        }
                    });

                    forgot_dialog.show();
            }

        });

这是我的数据库适配器类:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class LoginDataBaseAdapter 
{
        static final String DATABASE_NAME = "login.db";
        static final int DATABASE_VERSION = 1;
        public static final int NAME_COLUMN = 1;
        // TODO: Create public field for each column in your table.
        // SQL Statement to create a new database.
        static final String DATABASE_CREATE = "create table "+"LOGIN"+
                                     "( " +"ID"+" integer primary key autoincrement,"+"USERNAME  text UNIQUE,PASSWORD text,SECHINT text); ";
        // Variable to hold the database instance
        public  SQLiteDatabase db;
        // Context of the application using the database.
        private final Context context;
        // Database open/upgrade helper
        private DataBaseHelper dbHelper;
        public  LoginDataBaseAdapter(Context _context) 
        {
            context = _context;
            dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public  LoginDataBaseAdapter open() throws SQLException 
        {
            db = dbHelper.getWritableDatabase();
            return this;
        }
        public void close() 
        {
            db.close();
        }

        public  SQLiteDatabase getDatabaseInstance()
        {
            return db;
        }

        public void insertEntry(String dialog_user_name_string,String dialog_pasword_string,String dialog_seq_answer)
        {
           ContentValues newValues = new ContentValues();

            newValues.put("USERNAME", dialog_user_name_string);
            newValues.put("PASSWORD",dialog_pasword_string);
            newValues.put("SECHINT", dialog_seq_answer);
            // Insert the row into your table
            db.insert("LOGIN", null, newValues);
            ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
        }
        public int deleteEntry(String dialog_pasword_string)
        {
            //String id=String.valueOf(ID);
            String where="USERNAME=?";
            int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{dialog_pasword_string}) ;
           // Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
            return numberOFEntriesDeleted;
        }   
        public String getSinlgeEntry(String userName)
        {
            Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
            if(cursor.getCount()<1) // UserName Not Exist
            {
                cursor.close();
                return "NOT EXIST";
            }
            cursor.moveToFirst();
            String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
            cursor.close();
            return password;                
        }
        public void  updateEntry(String forgot_new_password_string)
        {
            // Define the updated row content.
            ContentValues updatedValues = new ContentValues();
            // Assign values for each row.
            //updatedValues.put("USERNAME", dialog_user_name_string);
            updatedValues.put("PASSWORD",forgot_new_password_string);


            String where="PASSWORD = ?";
            db.update("LOGIN",updatedValues, where, new String[]{forgot_new_password_string});             
        }       
}

这是我的logcat:

02-17 18:24:02.590: E/AndroidRuntime(29035): FATAL EXCEPTION: main
    02-17 18:24:02.590: E/AndroidRuntime(29035): Process: com.medequip.c1testpanel, PID: 29035
    02-17 18:24:02.590: E/AndroidRuntime(29035): java.lang.IllegalArgumentException: the bind value at index 1 is null
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1339)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1186)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1057)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1225)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at com.medequip.c1testpanel.LoginDataBaseAdapter.getSinlgeEntry(LoginDataBaseAdapter.java:65)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at com.medequip.c1testpanel.Login_Page$2$1.onClick(Login_Page.java:144)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at com.gc.materialdesign.views.ButtonFlat.onDraw(ButtonFlat.java:83)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.draw(View.java:14728)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13588)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13635)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13509)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13635)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13509)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13635)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13509)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13635)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13509)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13635)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3226)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13509)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.View.getDisplayList(View.java:13635)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1627)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1506)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2722)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2587)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2210)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.Choreographer.doFrame(Choreographer.java:560)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.os.Handler.handleCallback(Handler.java:808)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.os.Handler.dispatchMessage(Handler.java:103)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.os.Looper.loop(Looper.java:193)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at android.app.ActivityThread.main(ActivityThread.java:5292)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at java.lang.reflect.Method.invoke(Method.java:515)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    02-17 18:24:02.590: E/AndroidRuntime(29035):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:6)

简单地说,你在selectionArgs中传递的值是null。确保传递的值不为null。请在执行此查询之前检查用户名变量的值

if(userName!=null){
    Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
}

在getSinlgeEntry(String userName)方法中。希望它有帮助。

答案 1 :(得分:0)

你在没有领先空间的情况下尝试过吗?

    public String getSinlgeEntry(String userName)
    {
        Cursor cursor=db.query("LOGIN", null, "USERNAME=?", new String[]{userName}, null, null, null);
        if(cursor.moveToFirst())
        {
            return cursor.getString(cursor.getColumnIndex("PASSWORD"));
        }
        cursor.close();
        return "NOT EXIST";        
    }