我是一个菜鸟,我使用搜索功能得到答案,但我没有找到任何东西,所以我在Android工作室的代码是我的nexus 7作为一个真实的设备来测试它,但每次我尝试运行它,我的nexus上出现一条消息,表示它已直接结束:S
通过单击按钮,数字将添加到Textview中,这是计算器的基础
这是我的代码:
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int actuel;
Button btn0 = (Button) findViewById(R.id.btn0);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
Button btn4 = (Button) findViewById(R.id.btn4);
Button btn5 = (Button) findViewById(R.id.btn5);
Button btn6 = (Button) findViewById(R.id.btn6);
Button btn7 = (Button) findViewById(R.id.btn7);
Button btn8 = (Button) findViewById(R.id.btn8);
Button btn9 = (Button) findViewById(R.id.btn9);
TextView output = (TextView) findViewById(R.id.textView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNumbers(0,view);
}
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNumbers(1,view);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNumbers(2,view);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNumbers(3,view);
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNumbers(4,view);
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNumbers(5,view);
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNumbers(6,view);
}
});
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNumbers(7,view);
}
});
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNumbers(8,view);
}
});
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNumbers(9,view);
}
});
}
private final void addNumbers(int num, View view){
actuel = actuel * 10 + num;
output.setText(String.valueOf(actuel));
}
}
答案 0 :(得分:2)
将您的代码更新为:
public class MainActivity extends Activity {
int actuel;
Button btn0 = null;
Button btn1 = null;
Button btn2 = null;
Button btn3 = null;
Button btn4 = null;
Button btn5 = null;
Button btn6 = null;
Button btn7 = null;
Button btn8 = null;
Button btn9 = null;
TextView output = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0 = (Button) findViewById(R.id.btn0);
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);
output = (TextView) findViewById(R.id.textView);
...
您的应用崩溃了,因为您的所有btnX和输出变量都为空。