0

I have a login activity in my android application. It consists of two EditText widgets(Username, Password) and two buttons(Login,Register). The problem is when I click the buttons there's no response and no errors in my logcat. I've imported android.view.View.OnClickListener and I've set OnClickListeners.

The weird thing is, it worked perfectly when I used FragmentActivities/FragmentViews, I received JSON responses and logged in, but when I changed back to just "normal" activities it simply had no response. Any help on this, Maybe there's something I'm missing but not seeing? Code below.

Login.java

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class Login extends Activity implements OnClickListener
{
    private EditText user, pass;
    private Button mSubmit, mRegister;

    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();
    private static final String LOGIN_URL = "http://************/tappedin/login.php";

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);

        private EditText user, pass;
        private Button mSubmit, mRegister;

        mSubmit = (Button) findViewById(R.id.butLogin);
        mRegister = (Button) findViewById(R.id.butRegister);

        mSubmit.setOnClickListener(this);
        mRegister.setOnClickListener(this);
    }
}

@Override
public void onClick(View v) 
{
// TODO Auto-generated method stub
   switch (v.getId())
   {
      case R.id.butLogin:
         new AttemptLogin().execute();
         break;
      case R.id.butRegister:
         Intent i = new Intent(this, Register.class);
         startActivity(i);
         break;
      default:
         break;
    }
}
class AttemptLogin extends AsyncTask<String, String, String> 
{
        boolean failure = false;
        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("Loggin in...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args)  
        {
            int success;
            String username = user.getText().toString();
            String password = pass.getText().toString();
            try 
            {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));

                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, Profile.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;
        }
        protected void onPostExecute(String file_url) 
        {
           pDialog.dismiss();
           if (file_url != null)
           {
              Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();               
           }
        }
   }

}

login_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/AppTheme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/llogin"
tools:context="com.TappedIn.prjtappedin.MainActivity" >

<ImageView
    android:id="@+id/imvLogo"
    android:layout_width="wrap_content"
    android:layout_height="170dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/tappedin" />

<EditText
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imvLogo"
    android:layout_alignRight="@+id/imvLogo"
    android:layout_below="@+id/imvLogo"
    android:layout_marginTop="38dp"
    android:ems="10"
    android:hint="Username"
    android:singleLine="true" >
    <requestFocus />
 </EditText>

<Button
    android:id="@+id/butRegister"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/butLogin"
    android:layout_alignRight="@+id/butLogin"
    android:layout_below="@+id/butLogin"
    android:layout_marginTop="16dp"
    android:text="Register"/>

<TextView
    android:id="@+id/txvForgotpw"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/butRegister"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="11dp"
    android:autoLink="web"
    android:text="Forgot Password" 
    />

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/username"
    android:layout_alignRight="@+id/username"
    android:layout_below="@+id/username"
    android:layout_marginTop="13dp"
    android:ems="10"
    android:hint="Password"
    android:inputType="textPassword"
    android:lines="@integer/lines"
    android:singleLine="true" >

</EditText>

<Button
    android:id="@+id/butLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/password"
    android:layout_alignRight="@+id/password"
    android:layout_below="@+id/password"
    android:layout_marginTop="16dp"
    android:text="Login"/>
</RelativeLayout>

Register.java

package com.TappedIn.prjtappedin;

import android.app.Activity;
import android.os.Bundle;

public class Register extends Activity
{
    public void onCreateView(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);
    }
}

Register_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lregister"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical"
android:scrollbars="vertical" >


<EditText
    android:id="@+id/txtName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txtLastname"
    android:layout_alignRight="@+id/txtLastname"
    android:layout_below="@+id/imvLogo"
    android:ems="10"
    android:hint="First Name"
    android:inputType="textPersonName"
    android:singleLine="true" />

<EditText
    android:id="@+id/txtLastname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imvLogo"
    android:layout_alignRight="@+id/imvLogo"
    android:layout_below="@+id/txtName"
    android:ems="10"
    android:hint="Last Name"
    android:inputType="textPersonName"
    android:singleLine="true" />

<EditText
    android:id="@+id/Username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txtEmail"
    android:layout_alignRight="@+id/txtEmail"
    android:layout_below="@+id/txtEmail"
    android:ems="10"
    android:hint="Username"
    android:singleLine="true" />

<EditText
    android:id="@+id/txtPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Username"
    android:layout_alignRight="@+id/Username"
    android:layout_below="@+id/Username"
    android:ems="10"
    android:hint="Password"
    android:inputType="textPassword"
    android:singleLine="true" />

<EditText
    android:id="@+id/RPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txtPassword"
    android:layout_alignRight="@+id/txtPassword"
    android:layout_below="@+id/txtPassword"
    android:ems="10"
    android:hint="Re-type Password"
    android:inputType="textPassword"
    android:singleLine="true" />

<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/RPassword"
    android:layout_alignRight="@+id/RPassword"
    android:layout_below="@+id/RPassword"
    android:ems="10"
    android:hint="Location(City/Country)"
    android:singleLine="true" />

<Button
    android:id="@+id/btnRegister"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/autoCompleteTextView1"
    android:layout_alignRight="@+id/autoCompleteTextView1"
    android:layout_below="@+id/autoCompleteTextView1"
    android:text="Register" />

<EditText
    android:id="@+id/txtEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txtLastname"
    android:layout_alignRight="@+id/txtLastname"
    android:layout_below="@+id/txtLastname"
    android:ems="10"
    android:hint="Email Address"
    android:inputType="textEmailAddress"
    android:singleLine="true" />

<ImageView
    android:id="@+id/imvLogo"
    android:layout_width="match_parent"
    android:layout_height="170dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:src="@drawable/tappedin" />
</RelativeLayout>

MainActivity.java

package com.TappedIn.prjtappedin;

import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity
{
   @Override
   protected void onCreate(Bundle savedInstanceState) 
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.login_layout);
   }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</LinearLayout>

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.TappedIn.prjtappedin"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="20" />    
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme" >

    <activity
        android:name="MainActivity"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Holo.Light.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Login"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Holo.Light" >
    </activity>
    <activity
        android:name=".Register"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Holo.Light" >
    </activity>    
    <activity
        android:name=".Profile"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Holo.Light" >
    </activity>       
</application>
</manifest>
moDev
  • 5,248
  • 4
  • 33
  • 63
Zachs007
  • 69
  • 1
  • 9

3 Answers3

1

I fixed this by starting the whole project from scratch. I think the issue was with my MainActivity.java. In my new project I used Login.java as my main activity and it worked perfect. All that's changed is the name of my Main Activity which is now LoginActivity.java and inside this is the exact same code as my old Login.java class. I would love to know what the actual issue was, but it's working now. Thanks for the help guys.

My new code for my main activity:

LoginActivity.java

 import java.util.ArrayList;
 import java.util.List;

 import org.apache.http.NameValuePair;
 import org.apache.http.message.BasicNameValuePair;
 import org.json.JSONException;
 import org.json.JSONObject;

 import android.app.Activity;
 import android.app.ProgressDialog;
 import android.content.Intent;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;


 public class LoginActivity extends Activity implements OnClickListener
 {
     private EditText user, pass;
     private Button mSubmit, mRegister;

     private ProgressDialog pDialog;

     JSONParser jsonParser = new JSONParser();

     private static final String LOGIN_URL = "http://***.***.*.**.****/tappedin/login.php";


     private static final String TAG_SUCCESS = "success";
     private static final String TAG_MESSAGE = "message";

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

         user = (EditText)findViewById(R.id.username);
         pass = (EditText)findViewById(R.id.password);

         mSubmit = (Button)findViewById(R.id.butLogin);
         mRegister = (Button)findViewById(R.id.butRegister);

         mSubmit.setOnClickListener(this);
         mRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) 
    {
       // TODO Auto-generated method stub
       switch (v.getId()) 
       {
           case R.id.butLogin:
           new AttemptLogin().execute();
           break;
           case R.id.btnRegister:
           Intent i = new Intent(this, Register.class);
           startActivity(i);
           break;
           default:
           break;
       }
    }

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

    boolean failure = false;

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

    @Override
    protected String doInBackground(String... args) 
    {
        // TODO Auto-generated method stub
        int success;
        String username = user.getText().toString();
        String password = pass.getText().toString();
        try 
        {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", username));
            params.add(new BasicNameValuePair("password", password));

            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(LoginActivity.this, Profile.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;

    }
    protected void onPostExecute(String file_url) 
    {
        pDialog.dismiss();
        if (file_url != null)
        {
            Toast.makeText(LoginActivity.this, file_url, Toast.LENGTH_LONG).show();
        }

    }

  }

}`
Zachs007
  • 69
  • 1
  • 9
0

Try this:

mSubmit.setOnClickListener(new OnClickListener() {

    //put your function here - 
});
AstroCB
  • 12,337
  • 20
  • 57
  • 73
James Yeo
  • 116
  • 1
  • 3
  • 12
0

I would think that you need to implement the onClick() method inside the onClickListener like this:

public class Login extends Activity implements OnClickListener
{
    private EditText user, pass;
    private Button mSubmit, mRegister;

    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();
    private static final String LOGIN_URL = "http://************/tappedin/login.php";

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);

        private EditText user, pass;
        private Button mSubmit, mRegister;

        mSubmit = (Button) findViewById(R.id.butLogin);
        mRegister = (Button) findViewById(R.id.butRegister);

        mSubmit.setOnClickListener(this);
        mRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) 
    {
    // TODO Auto-generated method stub
       switch (v.getId())
       {
          case R.id.butLogin:
             new AttemptLogin().execute();
             break;
          case R.id.butRegister:
             Intent i = new Intent(this, Register.class);
             startActivity(i);
             break;
          default:
             break;
        }
    }
}

Otherwise it's as if you don't have the onClick() method at all, and it will never execute. In fact I'm surprised that you're not receiving an error that Login doesn't implement the necessary methods.

Akshay
  • 814
  • 6
  • 19
  • Thanks, I did exactly that, but still nothing happens. – Zachs007 Aug 29 '14 at 10:55
  • I have here: `mSubmit.setOnClickListener(this);` `mRegister.setOnClickListener(this);` – Zachs007 Aug 29 '14 at 13:23
  • @Zachs007 Sorry, I missed that. Putting `onClick()` inside `Login` was definitely necessary, but I'm not exactly sure what the new problem is. – Akshay Aug 29 '14 at 15:52
  • I've been having the same issue. A button works on some devices, not on others. When it doesn't work, I see a `Sending WAIT chunk` in the log. I just added a line to print to the logcat at the beginning of the activity, and things seem to work. This is quite baffling. – Debosmit Ray Mar 13 '16 at 12:12