Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Java
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);
                        
            //Set Content View to Login Layout
            setContentView(R.layout.login);
                
            //Call to CreateDatabase Function
            CreateDatabase();
            
            /*Check if SD Card is mounted*/
            if(!(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)))
            {
                /*If SD Card is not mounted, display an Alert and exit App. */
                ShowAlertDialog("SD Card not mounted. Please Insert SD Card and try again.");
                finish();
            }
            
            //Button Click Listener for Login button
            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);//Exception is being thrown when i try to start this activity
                                }       
                                else
                                {
                                    ShowAlertDialog("Incorrect login details");
                                }
                            }   
                            
                            if(c.isNull(0) == false)
                            {
                                ShowAlertDialog("Username doesn't exist");
                            }       
                        }
                        finally
                        {
                            dbCallLog.close();
                        }                       
                    }           
                }
            });
            
            //Button Click Listener for Signup button
            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()
    {       
        /*Check if Database exists , else create a Database. */
        dbCallLog = openOrCreateDatabase("calllog.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);

        /*Set the Database Version*/
        dbCallLog.setVersion(3); 
        
        /*Set the Database Locale*/
        dbCallLog.setLocale(Locale.getDefault());
        
        /*Enable Database Locking*/
        dbCallLog.setLockingEnabled(true);
        
        try
        {           
            /*Create a Table in the Database. */
            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();
    }
    
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900