简体   繁体   English

android编程的新手,试图创建登录按钮以进入登录屏幕。 当我单击登录按钮时,应用程序显示“停止工作”

[英]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 登录按钮所在的家庭 Activity

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. onClickListener用于单击登录按钮,并将用户带到登录页面。

    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. 这将是放置Click Listener的更正确的方法,而不是为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);
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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