Android点击按钮没有响应

时间:2014-08-28 20:15:22

标签: java android onclicklistener buttonclick no-response

我在Android应用程序中有一个登录活动。它由两个EditText widgets(Username, Password)two buttons(Login,Register)组成。问题是,当我单击按钮时,我的logcat中没有响应且没有错误。我已导入android.view.View.OnClickListener,我已设置OnClickListeners

奇怪的是,当我使用FragmentActivities/FragmentViews时,它完美运行,我收到了JSON响应并登录,但是当我改回到#34;正常"活动它根本没有回应。对此有任何帮助,也许有一些我不知道但却没有看到的东西?代码如下。

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>

清单

<?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>

3 个答案:

答案 0 :(得分:0)

试试这个:

mSubmit.setOnClickListener(new OnClickListener() {

    //put your function here - 
});

答案 1 :(得分:0)

我认为你需要在onClick()里面{/ 1}}实现onClickListener方法

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;
        }
    }
}

否则它就好像你根本没有onClick()方法,它永远不会执行。事实上,我很惊讶你没有收到Login没有实施必要方法的错误。

答案 2 :(得分:0)

我通过从头开始整个项目来解决这个问题。我认为问题出在我的MainActivity.java上。在我的新项目中,我使用Login.java作为我的主要活动,它完美无缺。 所有更改的是我的主要活动的名称,现在是LoginActivity.java,其中包含与我的旧Login.java类完全相同的代码。我很想知道实际问题是什么,但它现在正在工作。谢谢你的帮助。

我的主要活动的新代码:

<强> 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();
        }

    }

  }

}`