簡體   English   中英

Android單擊按鈕無響應

[英]Android on click button not responding

我的Android應用程序中有一個登錄活動。 它由兩個EditText widgets(Username, Password)two buttons(Login,Register) 問題是當我單擊按鈕時,logcat中沒有響應,也沒有錯誤。 我已經導入了android.view.View.OnClickListener並設置了OnClickListeners

奇怪的是,當我使用FragmentActivities/FragmentViews時,它工作得很好,我收到了JSON響應並登錄,但是當我回到“正常”活動時,它根本沒有響應。 任何幫助,也許我缺少但看不到的東西? 下面的代碼。

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>

注冊.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>

嘗試這個:

mSubmit.setOnClickListener(new OnClickListener() {

    //put your function here - 
});

我認為您需要像這樣在onClickListener 內部實現onClick()方法:

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未實現必要方法的錯誤。

我通過從頭開始整個項目來解決此問題。 我認為問題出在我的MainActivity.java 在我的新項目中,我使用Login.java作為主要活動,並且運行良好。 更改的只是我的Main Activity的名稱,現在是LoginActivity.java並且其中的代碼與舊的Login.javaLogin.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();
        }

    }

  }

}`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM