简体   繁体   中英

New to android programming, trying to create login button that leads to login screen. When I click the login button, the app says “stopped working”

The Home Activity where the login button is

package com.example.james.assignment1_18094969;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;

public class Home extends AppCompatActivity {

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

        //findview for the login button 
        findViewById(R.id.button_login).setOnClickListener(new login());
    }

onClickListener for the login button to be clicked and take the user to the login page.

    class login implements OnClickListener {
        public void onClick(View v) {
            Intent intent = new Intent(Home.this, login.class);
            startActivity(intent);
        }
    }
}

The login screen:

package com.example.james.assignment1_18094969;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Login extends AppCompatActivity {

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

Try This and please Confirm Login Activity is declared in the manifest

    package com.example.james.assignment1_18094969;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View.OnClickListener;
    import android.view.View;
    import android.content.Intent;

    public class Home extends AppCompatActivity implements View.OnClickListener {

        Button Login;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);
            Login=findViewById(R.id.button_login);
            login.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Home.this, Login.class);
            startActivity(intent);
        } 
}

this will be more correct approach to put click Listener's instead of defining inner class for click Listener. Or you can use functions instead.

public class Home extends AppCompatActivity {

Button loginButton;

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

    loginButton = (Button) findViewById(R.id.button_login);

    loginButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Home.this, Login.class);
            startActivity(intent);
        } 

    });
}

Acitivity names should be used correctly.

Do this!

findViewById(R.id.button_login).setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
       Intent intent = new Intent(Home.this, Login.class);
       startActivity(intent);
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM