大佬教程收集整理的这篇文章主要介绍了为什么android应用程序没有启动?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的 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://scheR_826_11845@as.androID.com/apk/res/androID"
xmlns:app="http://scheR_826_11845@as.androID.com/apk/res-auto"
xmlns:tools="http://scheR_826_11845@as.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>
这里忘记定义btnEqual了。 添加这一行
btnEqual = (Button)findViewById(R.id.btnEqual);
,
将 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
}
}
以上是大佬教程为你收集整理的为什么android应用程序没有启动?全部内容,希望文章能够帮你解决为什么android应用程序没有启动?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。