0

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

奇怪的是,当我使用它时它运行良好FragmentActivities/FragmentViews,我收到 JSON 响应并登录,但是当我改回“正常”活动时,它根本没有响应。对此有任何帮助,也许我缺少某些东西但没有看到?代码如下。

登录.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);
    }
}

注册布局

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

3 回答 3

1

我通过从头开始整个项目来解决这个问题。我认为问题出在我的MainActivity.java. 在我的新项目中,我将Login.java其用作主要活动,并且效果很好。改变的只是我的 Main Activity 的名称,它现在和里面的代码与我的旧课程LoginActivity.java完全相同。Login.java我很想知道实际的问题是什么,但它现在正在工作。谢谢你们的帮助。

我的主要活动的新代码:

登录活动.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();
        }

    }

  }

}`
于 2014-08-29T13:13:16.517 回答
0

尝试这个:

mSubmit.setOnClickListener(new OnClickListener() {

    //put your function here - 
});
于 2014-08-29T00:56:33.190 回答
0

我认为您需要像这样在内部实现该onClick()方法: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未实现必要方法的错误。

于 2014-08-29T01:00:19.820 回答