按钮onClick()导致强制停止

问题描述 投票:0回答:2

你好StackOverFlowGeeks!目前,我正在学习如何编写一个基本的Android应用程序,它只有很少的按钮。我有四个活动(Java类)和四个xmls。

我有RegisterScreen.java有两个按钮(注册和后退按钮)。目前,我正试图从这个RegisterScreen.java返回到SignInScreen.java。问题是,当我点击后退按钮时会导致失败并且应用程序被强制关闭...所以我上传了这种方式:1)RegisterScreen.java 2)SignInScreen.java 3)RegisterScreen XML文件4 )SignInScreen XML文件提前感谢。

RegisterScreen:

public class RegisterScreen extends AppCompatActivity {

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


    public void onClickRegisterButton(View view){

    }

    public void onClickBackButton(View view) {
        Button backBtn = (Button) findViewById(R.id.backBtn);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterScreen.this, SignInScreen.class);
                RegisterScreen.this.startActivity(intent);
            }
        });
    }
}

SignInScreen:

public class SignInScreen extends AppCompatActivity {

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

    @SuppressLint("SetTextI18n")
    public void onClickSignInButton(View view){

        TextView username = (TextView)findViewById(R.id.username_text);
        TextView password = (TextView)findViewById(R.id.password_text);
        Button signIn = (Button)findViewById(R.id.sign_in_button);

        if (username.getText().toString().equals("Ivan Simeonov") && password.getText().toString().equals("Ivan9603116245")){
            signIn.setText("Welcome " + username.getText().toString());
            signIn.setBackgroundColor(Color.YELLOW);
            setContentView(R.layout.activity_logged_screen);
        }else{
            signIn.setText("The input data is incorrect! Try again!");
            signIn.setBackgroundColor(Color.GREEN);
        }
    }

    public void onClickRegisterButton(View view){
        Button register = (Button)findViewById(R.id.register_button);
        setContentView(R.layout.activity_register_screen);
    }

    public void onClickResetPasswordButton(View view){
        Button resetPassword = (Button)findViewById(R.id.reset_password_button);
        setContentView(R.layout.activity_reset_password_screen);
    }
}

   <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.megat0n.startproject.RegisterScreen">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView_Register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/register_your_account_by_filling_up_the_following_data"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/enter_username"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/first_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/enter_firstname"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/last_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/enter_lastname"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/birth_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/enter_birthdate"
        android:inputType="date" />

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/enter_email"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/email_confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/confirm_email"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/enter_password"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/password_confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/confirm_password"
        android:inputType="textPassword" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/reg_account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/register"
            android:onClick="onClickRegisterButton"
            android:textAlignment="center"
            android:textAllCaps="false"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/backBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/back"
            android:onClick="onClickBackButton"
            android:textAlignment="center"
            android:textAllCaps="false"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.megat0n.startproject.SignInScreen">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:text="@string/welcome_to_the_home_screen"
            android:textAlignment="center"
            android:textColor="@android:color/black"
            android:textSize="24sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/username_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/username"
            android:inputType="textPersonName"
            android:textAlignment="center"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/password_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/password"
            android:inputType="textPassword"
            android:textAlignment="center"
            android:textStyle="bold" />

        <Button
            android:id="@+id/sign_in_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClickSignInButton"
            android:text="@string/sign_in"
            android:textAlignment="center"
            android:textAllCaps="false"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/register_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClickRegisterButton"
            android:text="@string/register"
            android:textAlignment="center"
            android:textAllCaps="false"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/reset_password_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClickResetPasswordButton"
            android:text="@string/reset_password"
            android:textAlignment="center"
            android:textAllCaps="false"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>
java android
2个回答
2
投票

使用此onClickBackButton()而不是你的。如果在布局中使用onClick属性,则不需要创建按钮并单击侦听器。

public void onClickBackButton(View view) {
   Intent intent = new Intent(MainActivity.this, Main2Activity.class);
   startActivity(intent);
}

-1
投票

尝试在RegisterScreen中使用onBackPressed()方法:

    backBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onBackPressed();
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.