初学Android程序员,按钮不工作?

时间:2014-03-08 15:51:31

标签: android

我正在使用eclipse和最新的sdk。

基本上我正在尝试生成一个登录/注册脚本,到目前为止我的应用程序正确加载并显示正确的布局,我可以填写详细信息,但我的注册或登录按钮都不起作用。

我需要做的是在我的MainActivity中的某个地方注册我的登录类,但是我不知道我会怎么做并让我的班级分开?

主要活动:

@SuppressWarnings("unused")
public class MainActivity extends Activity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.welcome_screen);

    }



}

登录

public class Login extends Activity implements OnClickListener
{

    private EditText EmailAddress, Password;
    private Button mSubmit, mRegister;
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();

    private static final String LOGIN_URL = "http://www.hotelwifiscore.com/app/login.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    private static final String TAG_UserID = "User_ID";
    private static final String TAG_Premium = "Premium";

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

            //setup input fields
            EmailAddress = (EditText)findViewById(R.id.email_Address);
            Password = (EditText)findViewById(R.id.password);

            //setup buttons
            mSubmit = (Button)findViewById(R.id.login);
            mRegister = (Button)findViewById(R.id.register);

            //register listeners
            mSubmit.setOnClickListener(this);
            mRegister.setOnClickListener(this);

        }

    @Override
    public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.login:
                    new AttemptLogin().execute();
            break;
            case R.id.register:
                    new CreateUser().execute();
            break;
            default:
            break;
        }
}

AttemptLogin:

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


        boolean failure = false;

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

        @Override
        protected String doInBackground(String... args) 
            {
                int success;
                String EmailA = EmailAddress.getText().toString();
                String PassW = Password.getText().toString();
            try 
                {

                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("Email_Address", EmailA));
                    params.add(new BasicNameValuePair("Password", PassW));

                    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, UserHome.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;


        }
    }

CREATEUSER:

    class CreateUser extends AsyncTask<String, String, String> 
    {
            boolean failure = false;

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

            @Override
            protected String doInBackground(String... args) 
                {

                int success;
                String EmailA = EmailAddress.getText().toString();
                String PassW = Password.getText().toString();
                try 
                    {

                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("Email_Address", EmailA));
                        params.add(new BasicNameValuePair("Password", PassW));

                        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("User Created!", json.toString());                
                            finish();
                            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;

            }
    }


        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) 
        {

            pDialog.dismiss();
            if (file_url != null)
            {
                Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
            }

        }
}

基本上没有任何反应!没有错误或什么?

任何指针都会非常感激: - )

干杯

4 个答案:

答案 0 :(得分:2)

在Login类的onClick方法中执行以下更改

......
@Override
public void onClick(View v) 
    {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.login:
            new AttemptLogin().execute();
            Intent intent = new Intent(this, "Name of the activity u want to go".class);
            startActivity(intent);

        break;
        case R.id.register:
            new CreateUser().execute();
            Intent intent = new Intent(this, "Name of the activity u want to go".class);
            startActivity(intent);

        break;
        default:
        break;
    }

答案 1 :(得分:1)

如果你想从另一个开始一个Activity,你必须在MainActivity.onCreate()中执行此操作:

Intent intent = new Intent(this, Login.class);
startActivity(intent);

我认为这就是你想要实现的目标。

答案 2 :(得分:1)

如果您希望您的应用以Login活动开始,请在清单中声明它:

<activity
        android:label="Login"
        android:name=".Login" >
         <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 </activity>

答案 3 :(得分:0)

谢谢大家,我决定转储我自己的登录并使用各种社交网络sdks!我使用了EarlofEgo和Deacons答案的组合,这确实解决了当前的问题,但我遇到了更多的负载!