Advertisement
Guest User

Untitled

a guest
Mar 10th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. E/AndroidRuntime(531): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.LoginScr.Example/com.LoginScr.Example.LoginDB}: java.lang.InstantiationException: can't instantiate class com.LoginScr.Example.LoginDB; no empty constructor
  2.  
  3. public class LoginDB extends SQLiteOpenHelper {
  4.  
  5. //Table attributes
  6. public static final String DATABASE_NAME = "logindata.db";
  7. public static final int DATABASE_VERSION = 1;
  8. public static final String TABLE_NAME_INFOTABLE = "credentials";
  9.  
  10. // Data attributes
  11. public static final String COLUMN_NAME_USERNAME = "username";
  12. public static final String COLUMN_NAME_PASSWORD = "password";
  13.  
  14. private SQLiteOpenHelper DBHelper;
  15. private SQLiteDatabase db;
  16.  
  17.  
  18.  
  19. public LoginDB(Context context) {
  20. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  21. // TODO Auto-generated constructor stub
  22. }
  23.  
  24. @Override
  25. public void onCreate(SQLiteDatabase db) {
  26. // TODO Auto-generated method stub
  27.  
  28. String sqlDataStore = "create table if not exists " +
  29. DATABASE_NAME + " ("+ BaseColumns._ID + " integer primary key autoincrement,"
  30.  
  31. + COLUMN_NAME_USERNAME + " text not null,"
  32. + COLUMN_NAME_PASSWORD + " text not null);";
  33.  
  34. db.execSQL(sqlDataStore);
  35. }
  36.  
  37. @Override
  38. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  39. // TODO Auto-generated method stub
  40. if(oldVersion == 1 && newVersion == 2){
  41. //Upgrade the database
  42. }
  43.  
  44. }
  45.  
  46. public boolean Login(String username, String password) throws SQLException
  47. {
  48. Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_NAME + " WHERE username=? AND password=?",
  49. new String[]{username,password});
  50. if(mCursor !=null) {
  51. if(mCursor.getCount()>0)
  52. {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58.  
  59. public void open() {
  60. // TODO Auto-generated method stub
  61. db = DBHelper.getWritableDatabase();
  62. }
  63. public void close() {
  64. DBHelper.close();
  65. }
  66. }
  67.  
  68. public class CredentialsActivity extends Activity implements OnClickListener{
  69.  
  70. private Button lReg;
  71. private EditText lUname;
  72. private EditText lPword;
  73.  
  74. private LoginDB loginDb = new LoginDB(CredentialsActivity.this);
  75.  
  76. @Override
  77. protected void onCreate (Bundle savedInstanceState) {
  78. // TODO Auto-generated method stub
  79. super.onCreate(savedInstanceState);
  80. setContentView(R.layout.register);
  81.  
  82. lReg = (Button)findViewById(R.id.reg_button);
  83. lReg.setOnClickListener(this);
  84. }
  85.  
  86. public void onClick(View v) {
  87.  
  88. switch(v.getId()) {
  89.  
  90. case R.id.reg_button:
  91. lUname = (EditText)findViewById(R.id.reg_uname);
  92. lPword = (EditText)findViewById(R.id.reg_pswd);
  93.  
  94. String uname = lUname.getText().toString();
  95. String pass = lPword.getText().toString();
  96. boolean invalid = false;
  97.  
  98. if(uname.equals("")){
  99. invalid = true;
  100. Toast.makeText(getApplicationContext(), "Username Missing", Toast.LENGTH_SHORT).show();
  101. }else if(pass.equals("")){
  102. invalid = true;
  103. Toast.makeText(getApplicationContext(), "Password Missing", Toast.LENGTH_SHORT).show();
  104. }
  105. if(invalid == false){
  106. addEntry(uname, pass);
  107. Intent i_register = new Intent(CredentialsActivity.this, LoginDB.class);
  108. startActivity(i_register);
  109. finish();
  110. }}
  111. }
  112. public void onDestroy() {
  113. super.onDestroy();
  114. loginDb.close();
  115. }
  116.  
  117. public void addEntry(String uname, String pass){
  118.  
  119. SQLiteDatabase db = loginDb.getWritableDatabase();
  120. ContentValues values = new ContentValues();
  121. values.put("username", uname);
  122. values.put("password", pass);
  123.  
  124. try{
  125. db.insert(LoginDB.DATABASE_NAME, null, values);
  126. Toast.makeText(getApplicationContext(), "Saved! Please login now", Toast.LENGTH_SHORT).show();
  127. }catch(Exception err){
  128. err.printStackTrace();
  129. }
  130. }
  131. }
  132.  
  133. private LoginDB loginDb = new LoginDB(CredentialsActivity.this);
  134.  
  135. private LoginDB loginDb;
  136. void onCreate(Bundle savedInstanceState)
  137. {
  138. super.onCreate(savedInstanceState);
  139. loginDb = new LoginDB(CredentialsActivity.this);
  140. ...
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement