The following is one of the files where in I user is authenticated and logged in.
I am getting an exception when i try to start another activity after authenticating user.
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity
{
String strUsername, strPassword;
SQLiteDatabase dbCallLog;
Intent intent;
@Override
public void onCreate(Bundle savedInstanceState)
{
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
CreateDatabase();
if(!(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)))
{
ShowAlertDialog("SD Card not mounted. Please Insert SD Card and try again.");
finish();
}
Button cmdLogin = (Button)findViewById(R.id.btnLogin);
cmdLogin.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v1)
{
EditText username = (EditText)findViewById(R.id.txtLUsername);
EditText password = (EditText)findViewById(R.id.txtLPassword);
String Uname = username.getEditableText().toString();
String UPassword = password.getEditableText().toString();
if(username.length()<1 || password.length()<1)
{
ShowAlertDialog("All details must be entered");
}
else
{
try
{
dbCallLog = openOrCreateDatabase("calllog.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
dbCallLog.setVersion(3);
dbCallLog.setLocale(Locale.getDefault());
dbCallLog.setLockingEnabled(true);
Cursor c = dbCallLog.query("users", null, null, null, null, null, null);
while(c.moveToNext())
{
String s1 = c.getString(3);
String s2 = c.getString(4);
if((s1.equals(Uname)) && (s2.equals(UPassword)))
{
intent = new Intent(v1.getContext(), ApplicationScreen.class);
startActivityForResult(intent, 0);
}
else
{
ShowAlertDialog("Incorrect login details");
}
}
if(c.isNull(0) == false)
{
ShowAlertDialog("Username doesn't exist");
}
}
finally
{
dbCallLog.close();
}
}
}
});
Button cmdSignup = (Button)findViewById(R.id.btnSignup);
cmdSignup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v2)
{
Intent intent = new Intent(v2.getContext(), NewUserRegistration.class);
startActivityForResult(intent, 0);
}
});
}
catch(Exception e)
{
Log.e("Error", "Error", e);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
public void CreateDatabase()
{
dbCallLog = openOrCreateDatabase("calllog.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
dbCallLog.setVersion(3);
dbCallLog.setLocale(Locale.getDefault());
dbCallLog.setLockingEnabled(true);
try
{
final String strCommand = "create table users (_id integer primary key autoincrement,fullname text,email text,username text not null unique,password textl);";
dbCallLog.execSQL(strCommand);
}
catch(SQLException e)
{
Log.d("CreateDatabase()",e.toString());
}
finally
{
dbCallLog.close();
}
}
public void ShowAlertDialog(String strErrorMessage)
{
AlertDialog adMessage = new AlertDialog.Builder(this).create();
adMessage.setMessage(strErrorMessage);
adMessage.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
adMessage.show();
}
}