Забыл пароль в android с помощью SQLite - PullRequest
0 голосов
/ 04 августа 2020

Я забыл пароль, но получаю сообщение об ошибке. Моя база данных содержит данные.

Вот logcat:

"E / AndroidRuntime: FATAL EXCEPTION: main Process: com.example.admin.myapplication, PID: 8265 java .lang.NullPointerException: попытка вызвать виртуальный метод 'java .lang.Boolean com.example.admin.myapplication.DatabaseHelper.updatepwd (java .lang.String, java .lang.String)' для нулевого значения ссылка на объект в com.example.admin.myapplication.Reset $ 1.onClick (Reset. java: 39) в android .view.View.performClick (View. java: 4780) в android .view. Просмотрите $ PerformClick.run (View. java: 19866) в android .os.Handler.handleCallback (Handler. java: 739) в android .os.Handler.dispatchMessage (Handler. java: 95) в android .os.Looper.l oop (Looper. java: 135) в android .app.ActivityThread.main (ActivityThread. java: 5254) в java .lang. Reflect.Method.invoke (собственный метод) в java .lang.reflect.Method.invoke (Method. java: 372) в com. android .internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit. java: 903) на com. android .internal.os.ZygoteIni t.main (ZygoteInit. java: 698) "

Ниже приведен следующий код, который я использовал:

Reset. java

public class Reset extends AppCompatActivity {
TextView phone;
EditText pwd,cpwd;
Button resetbtn;
 DatabaseHelper db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset);

        phone = (TextView)findViewById(R.id.Phonenumber);
        pwd = (EditText) findViewById(R.id.pssword);
        cpwd = (EditText) findViewById(R.id.pwdreenter);
        resetbtn = (Button) findViewById(R.id.btnReset);
        Intent intent = getIntent();
        phone.setText(intent.getStringExtra("phone"));
resetbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
String user = phone.getText().toString();
                String pswd = pwd.getText().toString();
                String repswd = cpwd.getText().toString();

               if (pswd.equals(repswd)) {
                    Boolean pwd = db.updatepwd(user, pswd);
                    if (pwd) {
                        Intent intent1 = new Intent(getApplicationContext(), Login.class);
                        startActivity(intent1);
                        Toast.makeText(Reset.this, "Password Updated Successfully", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(Reset.this, "Password NotUpdated Successfully", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(getApplicationContext(), "Password Don't Match", Toast.LENGTH_SHORT).show();
                }
            }
        });

//new code
public class MyTestDatabaseInstanceHolder {

    public MyTestDBHandler DBHelper;  
    public static SQLiteDatabase m_ObjDataBase; 

    public static void createDBInstance(Context pContext){
        if(DBHelper == null) {
            DBHelper = new WLDBHandler(pContext);
            m_cObjDataBase = DBHelper.openAndCreateDataBase(); 
        }
      }
}

Дб. java

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "application.db";

    public static final String TABLE_SIGNUP = "Signup";

    public static final String COL_ID = "USER_ID";
    public static final String COL_NAME = "NAME";
    public static final String COL_PHONE = "PHONE_NUMBER";
    public static final String COL_EMAIL = "EMAIL";
    public static final String COL_PASSWORD = "PASSWORD";
    public static final String COL_CONFIRMPASSWORD = "CONFIRMPASSWORD";

    public static final String COL_NAMEone_CON = "NAMEONE";
    public static final String COL_NUMBERone_CON = "NUMBERONE";
    public static final String COL_NAMEtwo_CON = "NAMETWO";
    public static final String COL_NUMBERtwo_CON = "NUMBERTWO";
    public static final String COL_NAMEthree_CON = "NAMETHREE";
    public static final String COL_NUMBERthree_CON = "NUMBERTHREE";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 3);
//        SQLiteDatabase db = this.getWritableDatabase();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_SIGNUP + "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , "
                + COL_NAME + " VARCHAR(255) , " + COL_PHONE + "  VARCHAR (255)  UNIQUE  ," + COL_EMAIL + " VARCHAR (255) UNIQUE," + COL_PASSWORD + " VARCHAR (255), "
                + COL_CONFIRMPASSWORD + " VARCHAR (255)," + COL_NAMEone_CON + " VARCHAR (255), "
                + COL_NUMBERone_CON + "  VARCHAR (255)  UNIQUE ," + COL_NAMEtwo_CON + " VARCHAR (255)," + COL_NUMBERtwo_CON + "  VARCHAR (255) UNIQUE , "
                + COL_NAMEthree_CON + " VARCHAR (255)," + COL_NUMBERthree_CON + " VARCHAR (255)  UNIQUE " + ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SIGNUP);
        onCreate(db);
    }


    Boolean updatepwd(String PHONE_NUMBER, String PASSWORD) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("PASSWORD", PASSWORD);
       long res=  db.update("Signup", contentValues, "PHONE_NUMBER  = ?", new String[]{PHONE_NUMBER});
        db.close();

        if(res == -1)
            return false;
        else
            return true;
}
...