从用户和&将它与使用Android中的SQLite数据库存储的数据进行比较?

时间:2018-02-11 15:04:53

标签: java android sqlite

这是LOGIN活动。当我运行该应用程序。一切正常,启动画面和登录界面工作正常,但是当我在editText&中输入输入时按验证按钮,应用程序崩溃并提示我重新启动应用程序。

public class Login extends AppCompatActivity {

        EditText cnic1;
        EditText fcnic1;
        EditText bill1;
        ImageButton verify1;

        String cc;
        String fc;
        String bl;
        HashMap<String, String> user;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            requestWindowFeature(Window.FEATURE_NO_TITLE);
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

            setContentView(R.layout.activity_login);

            cnic1 = (EditText) findViewById(R.id.cnic);
            fcnic1 = (EditText) findViewById(R.id.fcnic);
            bill1 = (EditText) findViewById(R.id.bill);
            verify1 = (ImageButton) findViewById(R.id.verify);


            Database db = new Database(this);
            user = db.getUserDetails();
        }
//Function for verification button
        public void verification(View obj) {

            if (cnic1.getText().toString().isEmpty() && fcnic1.getText().toString().isEmpty() && bill1.getText().toString().isEmpty()) {
                Toast.makeText(this, "InComplete Credentials", Toast.LENGTH_SHORT).show();
            } else {
                String c = user.get("CNIC");
                String f= user.get("FCNIC");
                String s = user.get("Serial");
                cc = cnic1.getText().toString();
                fc = fcnic1.getText().toString();
                bl = bill1.getText().toString();
                if (c.equals(cc) && f.equals(fc) && s.equals(bl)) {
                    Intent z = new Intent(Login.this, Home.class);
                    startActivity(z);
                } else {
                    Toast.makeText(this, "Wrong Credentials", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

这是数据库类。我添加了你给我的代码。我不知道问题是在数据库还是登录中。因为这是我第一次尝试为我的应用程序创建数据库。

public class Database extends SQLiteOpenHelper {
    public Database(Context context) {
        super(context, "DB.db", null, 1);
        SQLiteDatabase db=this.getWritableDatabase();
    }
//Creating table here    
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("Create Table Data (CNIC INTEGER Primary Key, FCNIC INTEGER, Serial INTEGER)");
    }
//Adding upgrade code here
    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS Data");
        onCreate(db);
    }
//This is the code that i have no idea of because i am reading Hashmap first time
    public HashMap<String, String> getUserDetails() {
        HashMap<String, String> user = new HashMap<String, String>();
        String selectQuery = "SELECT  * FROM Data";

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // Move to first row
        cursor.moveToFirst();
        if (cursor.getCount() > 0) {
            user.put("CNIC", cursor.getString(1));
            user.put("FCNIC", cursor.getString(2));
            user.put("Serial", cursor.getString(3));
        }
        cursor.close();
        db.close();
        // return user
        Log.d("Log", "Fetching user from Sqlite: " + user.toString());
        return user;
    }
}

---

<!-- language: lang-none -->

    02-13 00:03:51.624 28891-28891/com.pbrigade.www.pakgeneralelections E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
    02-13 00:04:04.391 28891-28891/com.pbrigade.www.pakgeneralelections E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                          Process: com.pbrigade.www.pakgeneralelections, PID: 28891
                                                                                          java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                                              at com.pbrigade.www.pakgeneralelections.Login.verification(Login.java:93)
                                                                                              at com.pbrigade.www.pakgeneralelections.Login$1.onClick(Login.java:57)
                                                                                              at android.view.View.performClick(View.java:6205)
                                                                                              at android.view.View$PerformClick.run(View.java:23653)
                                                                                              at android.os.Handler.handleCallback(Handler.java:751)
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:6682)
                                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

1 个答案:

答案 0 :(得分:0)

activityMain

public class MainActivity extends AppCompatActivity {

    EditText cnic1;
    EditText fcnic1;
    EditText bill1;
    ImageButton verify1;


    boolean isUserFound;
    Database db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        cnic1 = (EditText) findViewById(R.id.cnic);
        fcnic1 = (EditText) findViewById(R.id.fcnic);
        bill1 = (EditText) findViewById(R.id.bill);
        verify1 = (ImageButton) findViewById(R.id.verify);



        db = new Database(this);


        //you forget onclick method
        verify1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                verification();
            }
        });

    }


    //Function for verification button
    public void verification()
    {
        if (cnic1.getText().toString().isEmpty() || fcnic1.getText().toString().isEmpty() || bill1.getText().toString().isEmpty()) {
            Toast.makeText(this, "Empty Credentials", Toast.LENGTH_SHORT).show();
            return;
        }
        else
        {
            isUserFound = db.getUserDetails(cnic1.getText().toString(),fcnic1.getText().toString(),bill1.getText().toString());
            if(isUserFound)
            {
                    Intent z = new Intent(MainActivity.this,hhActivity.class);
                    startActivity(z);
                }
                else
                {
                    Toast.makeText(this, "Wrong Credentials", Toast.LENGTH_SHORT).show();
                }
        }
    }
}

DB

    public class Database extends SQLiteOpenHelper {
    public Database(Context context) {
        super(context, "DB.db", null, 1);
        SQLiteDatabase db=this.getWritableDatabase();
    }
    //Creating table here
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("Create Table Data (CNIC INTEGER Primary Key,FCNIC TEXT, Serial TEXT)");
    }
    //Adding upgrade code here
    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS Data");
        onCreate(db);
    }
    //This is the code that i have no idea of because i am reading Hashmap first time
    public boolean getUserDetails(String cnic1, String fcn1, String serial)
    {
        String selectQuery = "SELECT  * FROM Data where CNIC = '"+cnic1+"' and FCNIC = '"+fcn1+"' and Serial = '"+serial+"' ";

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // Move to first row
        cursor.moveToFirst();
        Log.d("Log", "Fetching user from Sqlite: " + cursor.getCount());

        if (cursor.getCount() > 0)
        {
            cursor.close();
            db.close();
           return true;
        }
        else
        {
            cursor.close();
            db.close();
            return false;
        }
    }
}