I have a problem with my app when I try to login.
In fact, if I register (if the checkbox remerberMe
isChecked()
on register action, at the next connection the main activity is displayed otherwise the login activity appears) first, it works fine.
However, the Login
Activity doesn't work when it's called on the next connection after register action.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
setTitle(R.string.loginTitle);
// Importing all assets like buttons, text fields
inputUsername = (EditText) findViewById(R.id.username_value);
inputPassword = (EditText) findViewById(R.id.password_value);
btnLogin = (Button) findViewById(R.id.loginButton);
btnExit = (Button) findViewById(R.id.exitButton);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegister);
rememberMe = (CheckBox) findViewById(R.id.rememberNameRegister);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginAction();
}
});
// Link to Register Screen
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
public void LoginAction() {
Log.i("Login", "Login Action");
progressDialog = ProgressDialog.show(LoginActivity.this, null,
getResources().getString(R.string.loginProgressMessage), true);
// try {
runOnUiThread(new Runnable() {
String username = inputUsername.getText().toString();
String password = inputPassword.getText().toString();
@Override
public void run() {
Log.i("Login Run", "username " + username);
switch (username.length()) {
case 0:
blankUserName();
break;
}
switch (password.length()) {
case 0:
blankPassWord();
// thread.stop();
break;
}
try {
if (username.length() > 0 && password.length() > 0) {
Log.i("Login Run", "password " + password);
// Password pass = new Password(username, password);
DatabaseHelper db = new DatabaseHelper(
getApplicationContext());
int count = db.getRowCount();
Log.i("Login", "getRowCompte " + count);
if (count == 1) {
Log.i("Login","getLogin1 ");
// if (db.getLogin(username, password) == 1) {
if (db.Login(username, password)== true) {
Log.i("Login", "rememberMe.isChecked()");
if (rememberMe.isChecked() == true) {
statut = "on";
} else if (rememberMe.isChecked() == false) {
statut = "off";
}
Log.i("Login", "ok ischecked");
Log.i("Login","getRowCompteStat "+ db.getRowCountStat());
if (db.getRowCountStat() == 1) {
db.UpdateStatut(statut);
Log.i("Login", "getRowCompte " + count);
}
Toast.makeText(getApplicationContext(),
"Student Moyenne \n Bienvenu!",
Toast.LENGTH_LONG).show();
Log.i("Login Run", "Connecté");
Intent moy = new Intent(
getApplicationContext(),
MoyenneMain.class);
// Close all views before launching
// Dashboard
moy.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(moy);
finish();
db.close();
} else if (db.Login(username, password)==false){
// if (db.getLogin(username, password) == 0) {
// Log.i("Login Run", "faux password");
Toast.makeText(LoginActivity.this,"Invalid Username/Password",Toast.LENGTH_LONG).show();
Intent login = new Intent(getApplicationContext(),LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
finish();
db.close();
}
} else if (count == 0) {
Log.i("Login Run", "Enregistrez vous");
Toast.makeText(LoginActivity.this,
"Enregistrez vous!", Toast.LENGTH_SHORT)
.show();
Intent register = new Intent(
getApplicationContext(),
RegisterActivity.class);
register.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(register);
finish();
db.close();
}
}
} catch (Exception e) {
Log.i("Login Error1", e.getMessage());
Toast.makeText(LoginActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
});
// } catch (Exception e) {
// Thread.currentThread().destroy();
// Log.i("Login Error2", e.getMessage());
// Toast.makeText(LoginActivity.this, e.getMessage(),
// Toast.LENGTH_LONG).show();
// }
}
private void blankUserName() {
Toast.makeText(LoginActivity.this, "Entrez un nom Utilisateur SVP!",
Toast.LENGTH_SHORT).show();
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
finish();
}
private void blankPassWord() {
Toast.makeText(LoginActivity.this, "Entrez un mot de passe SVP!",
Toast.LENGTH_SHORT).show();
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
finish();
}
public int getRowCountStat() {
String countQuery = "SELECT * FROM " + TABLE_STATUT;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
// return row count
return rowCount;
}
public int getRowCount() {
String countQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
// return row count
return rowCount;
}
public boolean Login(String username, String password) throws SQLException
{
Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_LOGIN + " WHERE username=? AND password=?"
, new String[]{username,password});
if (mCursor != null) {
if(mCursor.getCount() > 0)
{
return true;
}
}
return false;
}
public int UpdateStatut(String statut) {
final static int idStat = 1;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(STATUT, statut);
return db.update(TABLE_STATUT, cv, ID_STAT + "=?",
new String[] { String.valueOf(idStat) });
}
10-24 01:48:08.819: E/AndroidRuntime(29242): FATAL EXCEPTION: main
10-24 01:48:08.819: E/AndroidRuntime(29242): java.lang.NullPointerException: println needs a message
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.util.Log.println_native(Native Method)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.util.Log.i(Log.java:143)
10-24 01:48:08.819: E/AndroidRuntime(29242): at com.android.moyenne.activity.LoginActivity$4.run(LoginActivity.java:163)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.app.Activity.runOnUiThread(Activity.java:3707)
10-24 01:48:08.819: E/AndroidRuntime(29242): at com.android.moyenne.activity.LoginActivity.LoginAction(LoginActivity.java:78)
10-24 01:48:08.819: E/AndroidRuntime(29242): at com.android.moyenne.activity.LoginActivity$1.onClick(LoginActivity.java:46)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.view.View.performClick(View.java:2408)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.view.View$PerformClick.run(View.java:8816)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.os.Handler.handleCallback(Handler.java:587)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.os.Looper.loop(Looper.java:123)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-24 01:48:08.819: E/AndroidRuntime(29242): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 01:48:08.819: E/AndroidRuntime(29242): at java.lang.reflect.Method.invoke(Method.java:521)
10-24 01:48:08.819: E/AndroidRuntime(29242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-24 01:48:08.819: E/AndroidRuntime(29242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-24 01:48:08.819: E/AndroidRuntime(29242): at dalvik.system.NativeStart.main(Native Method)
10-24 01:48:12.260: I/Process(29242): Sending signal. PID: 29242 SIG: 9
In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.
What is NullPointerException? It is a run-time exception that arises when an application or a program tries to access the object reference(accessing methods) which has a null value stored in it.
In the catch block e.getMessage()
may be null
. Try this:
String msg = (e.getMessage()==null)?"Login failed!":e.getMessage();
Log.i("Login Error1",msg);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With