Question

i'm new to android and i'm working on an update for my first application. The update is to put a log in screen in the beginning. i chose to use a new activity to do the check for the credentials entered and launch it first then if the username and password match, it'll take the user to the main activity. i wrote the code, but it's not working. When i send it to my phone it gives me the android message error as if it crashed. I quick heads up, my teacher wants the username and password to be saved as resources string arrays to simulate the database since we didn't learn that yet. Other options he suggested is controlling the views, using tabHost, or framLayout, i thought using another activity will be easier since activities are all i have worked with so far. If anybody can look at my code and tell me what i did wrong. Also note that i'm not asking to solve my homework for me, i want to learn. Thanks a lot guys.

import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // buttons variables
    Button signIn, exit , clear;


     final HashMap<String, String> hash= new HashMap<String, String>();
     hash.put(getString(R.array.usernameArray),getString(R.array.passwordArray));

  // text boxes variables
        final EditText userName = (EditText) findViewById(R.id.usernameEditText);
        userName.setEnabled(true);

        final EditText password = (EditText) findViewById(R.id.passwordEditText);
        password.setEnabled(true);

     // initializing the signin button
        signIn = (Button) findViewById(R.id.signInButton);
        signIn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int count = 3;
        EditText usrnm =   (EditText)findViewById(R.id.usernameEditText); 
        EditText pswd = (EditText)findViewById(R.id.passwordEditText);

                if(hash.containsKey(usrnm))
                {
                    String val = hash.get(usrnm);
                    if(val.equals(pswd)){

                    Toast.makeText(getApplicationContext(), 
                                "the values are matched", 
                                Toast.LENGTH_LONG).show();
Intent openMainActivity = new Intent("com.alijaouhari.paycalculator.MainActivity");
                        startActivity(openMainActivity);
                    }
                    else if(count>0)
                    {
    Toast.makeText(getApplicationContext(), 
     "Wrong Credentials, Please check your usename and password and try again", 
                        Toast.LENGTH_LONG).show();
                        count--;
                    }

                }

            }
        }); // end signin button



        // initializing the exit button
        exit = (Button) findViewById(R.id.exitButton);
        exit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        }); // end exit button

        // initializing the clear button
        clear = (Button) findViewById(R.id.clearButton);
        clear.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
// when clicking the clear button, it sets all the text edit boxes to an empty string
                userName.setText("");
                password.setText("");


            }
        });// end clear button

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
Was it helpful?

Solution

You need to add the right intent filters for your activity in the Manifest xml file.

If 'MainActivity' is the name of your activity that you want to start first when the app opens, add the following to the manifest file. Remember that you have these (i.e. DEFAULT & LAUNCHER) category fields in the intent-filter only for a single activity in your application.

<activity
        android:name="com.example.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top