dave
dave

Reputation: 1430

Beginner Android Programmer, Buttons not working?

I am using eclipse and the latest sdk.

Basically I am trying to produce a login/register script, what happens so far is my app loads correctly and the correct layout is displayed, I can fill in the details but neither my register or login button works.

I think that what I need to do is register my login class somewhere in my MainActivity, but I am not sure how I would do this and keep my class seperate??

Main Activity:

@SuppressWarnings("unused")
public class MainActivity extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.welcome_screen);

    }



}

Login:

public class Login extends Activity implements OnClickListener
{

    private EditText EmailAddress, Password;
    private Button mSubmit, mRegister;
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();

    private static final String LOGIN_URL = "http://www.hotelwifiscore.com/app/login.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    private static final String TAG_UserID = "User_ID";
    private static final String TAG_Premium = "Premium";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.welcome_screen);

            //setup input fields
            EmailAddress = (EditText)findViewById(R.id.email_Address);
            Password = (EditText)findViewById(R.id.password);

            //setup buttons
            mSubmit = (Button)findViewById(R.id.login);
            mRegister = (Button)findViewById(R.id.register);

            //register listeners
            mSubmit.setOnClickListener(this);
            mRegister.setOnClickListener(this);

        }

    @Override
    public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.login:
                    new AttemptLogin().execute();
            break;
            case R.id.register:
                    new CreateUser().execute();
            break;
            default:
            break;
        }
}

AttemptLogin:

    class AttemptLogin extends AsyncTask<String, String, String> 
    {


        boolean failure = false;

        @Override
        protected void onPreExecute() 
            {
                super.onPreExecute();
                pDialog = new ProgressDialog(Login.this);
                pDialog.setMessage("Attempting login...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }

        @Override
        protected String doInBackground(String... args) 
            {
                int success;
                String EmailA = EmailAddress.getText().toString();
                String PassW = Password.getText().toString();
            try 
                {

                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("Email_Address", EmailA));
                    params.add(new BasicNameValuePair("Password", PassW));

                    Log.d("request!", "starting");

                    JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);

                    Log.d("Login attempt", json.toString());
                    success = json.getInt(TAG_SUCCESS);

                    if (success == 1) 
                        {
                            Log.d("Login Successful!", json.toString());
                            Intent i = new Intent(Login.this, UserHome.class);
                            finish();
                            startActivity(i);
                            return json.getString(TAG_MESSAGE);
                        }
                    else
                        {
                            Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                            return json.getString(TAG_MESSAGE);
                        }
                } 
            catch (JSONException e) 
                {
                    e.printStackTrace();
                }

            return null;


        }
    }

CreateUser:

    class CreateUser extends AsyncTask<String, String, String> 
    {
            boolean failure = false;

            @Override
            protected void onPreExecute() 
                {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(Login.this);
                    pDialog.setMessage("Creating User...");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                    pDialog.show();
                }

            @Override
            protected String doInBackground(String... args) 
                {

                int success;
                String EmailA = EmailAddress.getText().toString();
                String PassW = Password.getText().toString();
                try 
                    {

                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("Email_Address", EmailA));
                        params.add(new BasicNameValuePair("Password", PassW));

                        Log.d("request!", "starting");
                        JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);                    
                        Log.d("Login attempt", json.toString());

                        success = json.getInt(TAG_SUCCESS);

                    if (success == 1) 
                        {
                            Log.d("User Created!", json.toString());                
                            finish();
                            return json.getString(TAG_MESSAGE);
                        }
                    else
                        {
                            Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                            return json.getString(TAG_MESSAGE);

                        }
                    } 
                catch (JSONException e) 
                    {
                        e.printStackTrace();
                    }

                return null;

            }
    }


        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) 
        {

            pDialog.dismiss();
            if (file_url != null)
            {
                Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
            }

        }
}

Basically nothing happens! No Errors or anything??

Any pointers would be greatly appreciated :-)

Cheers

Upvotes: 0

Views: 95

Answers (4)

dave
dave

Reputation: 1430

Thanks all, I have decided to dump my own login and use the various social network sdks! I used a combo of EarlofEgo and Deacons answers and this did fix the immediate problem, but I run into a load more!

Upvotes: 0

ik024
ik024

Reputation: 156

Do the folloeing changes in ur onClick method of ur Login class

......
@Override
public void onClick(View v) 
    {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.login:
            new AttemptLogin().execute();
            Intent intent = new Intent(this, "Name of the activity u want to go".class);
            startActivity(intent);

        break;
        case R.id.register:
            new CreateUser().execute();
            Intent intent = new Intent(this, "Name of the activity u want to go".class);
            startActivity(intent);

        break;
        default:
        break;
    }

Upvotes: 2

Decoy
Decoy

Reputation: 1597

If you want your app to start with Login activity declare it in the manifest :

<activity
        android:label="Login"
        android:name=".Login" >
         <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 </activity>

Upvotes: 1

Stephan
Stephan

Reputation: 16789

If you want to start an Activity from another you have to do this in your MainActivity.onCreate():

Intent intent = new Intent(this, Login.class);
startActivity(intent);

I think that's what you are trying to achieve.

Upvotes: 1

Related Questions