为什么android应用程序没有启动?

时间:2021-02-07 05:00:11

标签: java android xml android-studio android-layout

这是我的 Mainactivity.java 代码。

在 mainactivity.java 文件中,如果我删除了 btnEqual.setOnClickListener 函数,那么它运行得很好,但是当我添加 btnEqual.setOnClickListener 函数时,应用程序没有打开。 我也尝试在 id btnEqual 上使用 onclick 功能,但它也不起作用。


    package com.example.calculator;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
    
        Button btnClear,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0,btnAdd,btnSub,btnDiv,btnEqual,btnDot,btnMult;
        EditText ed1;
        float Res1,Res2;
        boolean Add,Sub,Mul,Div;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn1 = (Button)findViewById(R.id.btn1);
            btn2 = (Button)findViewById(R.id.btn2);
            btn3 = (Button)findViewById(R.id.btn3);
            btn4 = (Button)findViewById(R.id.btn4);
            btn5 = (Button)findViewById(R.id.btn5);
            btn6 = (Button)findViewById(R.id.btn6);
            btn7 = (Button)findViewById(R.id.btn7);
            btn8 = (Button)findViewById(R.id.btn8);
            btn9 = (Button)findViewById(R.id.btn9);
            btn0 = (Button)findViewById(R.id.btn0);
            btnAdd = (Button)findViewById(R.id.btnAdd);
            btnSub = (Button)findViewById(R.id.btnSub);
            btnMult = (Button)findViewById(R.id.btnMult);
            btnDiv = (Button)findViewById(R.id.btnDiv);
            btnDot = (Button)findViewById(R.id.btnDot);
            btnClear = (Button)findViewById(R.id.btnClear);
            ed1 = (EditText)findViewById(R.id.editText);
    
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        ed1.setText(ed1.getText()+"1");
                }
            });
            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ed1.setText(ed1.getText()+"2");
                }
            });
            btn3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ed1.setText(ed1.getText()+"3");
                }
            });
            btn4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ed1.setText(ed1.getText()+"4");
                }
            });
            btn5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ed1.setText(ed1.getText()+"5");
                }
            });
            btn6.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ed1.setText(ed1.getText()+"6");
                }
            });
            btn7.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ed1.setText(ed1.getText()+"7");
                }
            });
            btn8.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ed1.setText(ed1.getText()+"8");
                }
            });
            btn9.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ed1.setText(ed1.getText()+"9");
                }
            });
            btn0.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ed1.setText(ed1.getText()+"0");
                }
            });
            btnDot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ed1.setText(ed1.getText()+".");
                }
            });
            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ed1 != null) {
                        Res1 = Float.parseFloat(ed1.getText()+"");
                        Add = true;
                        ed1.setText("");
                    } else {
                        ed1.setText("");
                    }
                }
            });
            btnSub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ed1 != null) {
                        Res1 = Float.parseFloat(ed1.getText()+"");
                        Sub = true;
                        ed1.setText("");
                    } else {
                        ed1.setText("");
                    }
                }
            });
            btnDiv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ed1 != null) {
                        Res1 = Float.parseFloat(ed1.getText()+"");
                        Div = true;
                        ed1.setText("");
                    } else {
                        ed1.setText("");
                    }
                }
            });
            btnMult.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ed1 != null) {
                        Res1 = Float.parseFloat(ed1.getText()+"");
                        Mul = true;
                        ed1.setText("");
                    } else {
                        ed1.setText("");
                    }
                }
            });
    
            btnEqual.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Res2 = Float.parseFloat(ed1.getText()+"");
                    
                    if(Add==true){
                        ed1.setText(Res1+Res2+"");
                        Add = false;
                    }
                    else if(Sub==true){
                        ed1.setText(Res1-Res2+"");
                        Sub = false;
                    }
                    else if(Div==true){
                        ed1.setText(Res1/Res2+"");
                        Div = false;
                    }
                    else if(Mul==true){
                        ed1.setText(Res1*Res2+"");
                        Mul = false;
                    }
                }
            });
            btnClear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ed1.setText("");
                }
            });
    
        }
    }

如果需要的话 这是我的 Activity_main.xml 代码。


    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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:id="@+id/b"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/btnDot"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="30dp"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:text="."
            app:layout_constraintEnd_toStartOf="@+id/btn0"
            app:layout_constraintHorizontal_bias="0.4"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btn7" />
    
        <Button
            android:id="@+id/btnDiv"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="36dp"
            android:layout_marginRight="36dp"
            android:text="/"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnMult" />
    
        <Button
            android:id="@+id/btnClear"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="36dp"
            android:layout_marginRight="36dp"
            android:text="clear"
            app:layout_constraintEnd_toStartOf="@+id/btnDiv"
            app:layout_constraintTop_toBottomOf="@+id/btn9" />
    
        <Button
            android:id="@+id/btn0"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:text="0"
            app:layout_constraintEnd_toStartOf="@+id/btnClear"
            app:layout_constraintTop_toBottomOf="@+id/btn8" />
    
        <Button
            android:id="@+id/btn7"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="30dp"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:text="7"
            app:layout_constraintEnd_toStartOf="@+id/btn8"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btn4" />
    
        <Button
            android:id="@+id/btnMult"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="36dp"
            android:layout_marginRight="36dp"
            android:text="*"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnSub" />
    
        <Button
            android:id="@+id/btn9"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:text="9"
            app:layout_constraintEnd_toStartOf="@+id/btnMult"
            app:layout_constraintTop_toBottomOf="@+id/btn6" />
    
        <Button
            android:id="@+id/btn5"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:text="5"
            app:layout_constraintEnd_toStartOf="@+id/btn6"
            app:layout_constraintTop_toBottomOf="@+id/btn2" />
    
        <Button
            android:id="@+id/btnSub"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="36dp"
            android:layout_marginRight="36dp"
            android:text="-"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnAdd" />
    
        <Button
            android:id="@+id/btn6"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:text="6"
            app:layout_constraintEnd_toStartOf="@+id/btnSub"
            app:layout_constraintTop_toBottomOf="@+id/btn3" />
    
        <Button
            android:id="@+id/btn1"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="268dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:text="1"
            app:layout_constraintEnd_toStartOf="@+id/btn2"
            app:layout_constraintHorizontal_bias="0.833"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/btn2"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="268dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:text="2"
            app:layout_constraintEnd_toStartOf="@+id/btn3"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/btn3"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="268dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:text="3"
            app:layout_constraintEnd_toStartOf="@+id/btnAdd"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/btnAdd"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="268dp"
            android:layout_marginEnd="36dp"
            android:layout_marginRight="36dp"
            android:text="+"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/btn4"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="30dp"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:text="4"
            app:layout_constraintEnd_toStartOf="@+id/btn5"
            app:layout_constraintHorizontal_bias="0.434"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btn1" />
    
        <Button
            android:id="@+id/btn8"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:text="8"
            app:layout_constraintEnd_toStartOf="@+id/btn9"
            app:layout_constraintTop_toBottomOf="@+id/btn5" />
    
        <EditText
            android:id="@+id/editText"
            android:layout_width="355dp"
            android:layout_height="50dp"
            android:layout_marginTop="75dp"
            android:ems="10"
            android:inputType="textPersonName"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_editor_absoluteX="25dp" />
    
        <Button
            android:id="@+id/btnEqual"
            android:layout_width="319dp"
            android:layout_height="56dp"
            android:layout_marginBottom="64dp"
            android:onClick="main"
            android:text="==="
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

这里忘记定义btnEqual了。 添加这一行

btnEqual = (Button)findViewById(R.id.btnEqual);

答案 1 :(得分:0)

<块引用>

将 btnEqual 视图绑定到它的对象,如下所示

btnEqual = findViewById(R.id.btnEqual);
<块引用>

此外,通过实现如下所示的 onclicklister,尝试使您的代码更高效、更干净

public class MainActivity extends AppCompatActivity implements View.OnClickListener 
{
   ...
}
<块引用>

将监听器设置为这样的按钮

btn1.setOnClickListener(this);
<块引用>

然后像下面那样覆盖 OnClick 方法

@Override
public void onClick(View view) {
    if(view.getId() == btnEqual.getId()){
       // PERFORM ACTION ON BUTTON HERE
     }
}