Android按钮不响应OnClickListener:

时间:2014-07-02 01:00:51

标签: android login android-studio httprequest onclicklistener

这是我在这里发表的第一篇文章......我正在学习android编程来制作应用程序。我尝试在线学习教程,首先让我的应用程序与我已设置为从SQL数据库登录用户的服务器进行通信。 但是在我的登录按钮似乎什么也没做。 这是LoginActivity:

    package com.tazo.zegga.app;

import ...


public class LoginActivity extends PlusBaseActivity implements LoaderCallbacks<Cursor>{
    String response = null;
    TextView  txt_Error;

    public void login (View view) {
        EditText txt_uname=(EditText)findViewById(R.id.email);
        EditText txt_pwd=(EditText)findViewById(R.id.password);

        String uname=txt_uname.getText().toString();
        String pwd=txt_pwd.getText().toString();
        UserLoginTask task = new UserLoginTask(uname, pwd);
        task.execute(uname, pwd);
    }
        public final static String EXTRA_RESULT = "com.tazo.zegga.result";

    // UI references.
    private AutoCompleteTextView mEmailView;
    private EditText mPasswordView;
    private View mProgressView;
    private View mEmailLoginFormView;
    private SignInButton mPlusSignInButton;
    private View mSignOutButtons;
    private View mLoginFormView;

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


        // Set up the login form.
        mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
        populateAutoComplete();

        mPasswordView = (EditText) findViewById(R.id.password);
        mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                if (id == R.id.login || id == EditorInfo.IME_NULL) {
                    attemptLogin();
                    return true;
                }
                return false;
            }
        });

        Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
        mEmailSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                attemptLogin();


            }
        });

        mLoginFormView = findViewById(R.id.login_form);
        mProgressView = findViewById(R.id.login_progress);
        mEmailLoginFormView = findViewById(R.id.email_login_form);
        mSignOutButtons = findViewById(R.id.plus_sign_out_buttons);
    }

    private void populateAutoComplete() {
        getLoaderManager().initLoader(0, null, this);
    }


    /**
     * Attempts to sign in or register the account specified by the login form.
     * If there are form errors (invalid email, missing fields, etc.), the
     * errors are presented and no actual login attempt is made.
     */
    public void attemptLogin() {

        if (mAuthTask != null) {
            return;
        }


        // Reset errors.
        mEmailView.setError(null);
        mPasswordView.setError(null);

        // Store values at the time of the login attempt.
        String email = mEmailView.getText().toString();
        String password = mPasswordView.getText().toString();

        boolean cancel = false;
        View focusView = null;

        UserLoginTask task = new UserLoginTask(email,password);
        task.execute(email, password);




    }
    private boolean isEmailValid(String email) {
        //TODO: Replace this with your own logic
        return email.contains("@");
    }

    private boolean isPasswordValid(String password) {
        //TODO: Replace this with your own logic
        return password.length() > 4;
    }



    /**
     * Represents an asynchronous login/registration task used to authenticate
     * the user.
     */
    public class UserLoginTask extends AsyncTask<String, Void, String> {

        private final String mEmail;
        private final String mPassword;


        UserLoginTask(String email, String password) {
            mEmail = email;
            mPassword = password;
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO: attempt authentication against a network service.
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("username", params[0] ));
            postParameters.add(new BasicNameValuePair("password", params[1] ));
            String res = null;
            try {
                response = CustomHttpClient.executeHttpPost("http://192.168.1.50/zegga/v1/index.php/login", postParameters);
                res=response.toString();
                res= res.replaceAll("\\s+","");
            }
            catch (Exception e) {
                txt_Error.setText(e.toString());
            }
            return res;
        }//close doInBackground


        }


        protected void onPostExecute(String result) {
            mAuthTask = null;
            showProgress(false);
            Intent intentLogin = new Intent(LoginActivity.this, MainActivity.class);
            intentLogin.putExtra(EXTRA_RESULT, result);
            startActivity(intentLogin);

        }


        protected void onCancelled() {
            mAuthTask = null;
            showProgress(false);
        }
    }

提前致谢

编辑:删除了与G +登录相关的代码...感谢CodeMagic指出我的代码大部分都不需要......

1 个答案:

答案 0 :(得分:2)

在mPlusSignInButton按钮的OnClickListener中,您调用了signIn()方法。但是,没有这样的方法。

如果您希望用户通过Google+登录,而不是使用您的登录按钮,则应使用Google+登录按钮。

请参阅this link以在Android应用中实施Google+登录按钮。