我试图写一个代码,以使一个简单的计算器,但模拟器不工作!它给了我这个消息“对不起,应用Task_6(过程com.example.task_6)已意外停止,请重试。”动态按钮onClickListener
这是来自logcat的味精: “在dalvik.system.NativeStart。主(本地梅托德)”当我点击按钮(+)或退格键:\\
布局是一样的东西CAL窗口:
这是我的代码,我不知道是否有不对的代码:\!
package com.example.task_6;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
EditText textBox;
Button b1,b2,b3,b4,b5,b6,b7;
int number_1,number_2,result,temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox= (EditText) findViewById(R.id.TextBox);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.Button01);
b3=(Button)findViewById(R.id.button2);
b4=(Button)findViewById(R.id.button3);
b5=(Button)findViewById(R.id.button4);
b6=(Button)findViewById(R.id.button5);
b7=(Button)findViewById(R.id.button6);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
}
public void onClick(View arg) {
if(arg==b1){
textBox.setText("");
}
if(arg==b2){
temp=Integer.parseInt(textBox.getText().toString().substring(0, textBox.length()-1));
textBox.setText(temp);
}
if(arg==b3){
number_1=Integer.parseInt(textBox.getText().toString());
textBox.setText("");
number_2=Integer.parseInt(textBox.getText().toString());
result=number_1+number_2;
}
if(arg==b4){
number_1=Integer.parseInt(textBox.getText().toString());
textBox.setText("");
number_2=Integer.parseInt(textBox.getText().toString());
result=number_1-number_2;
}
if(arg==b5){
textBox.setText(result);
}
if(arg==b6){
number_1=Integer.parseInt(textBox.getText().toString());
textBox.setText("");
number_2=Integer.parseInt(textBox.getText().toString());
result=number_1*number_2;
}
if(arg==b7){
number_1=Integer.parseInt(textBox.getText().toString());
textBox.setText("");
number_2=Integer.parseInt(textBox.getText().toString());
result=number_1/number_2;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
发表您的错误日志吗? – juned
阅读logcat消息,如果您无法解决问题,请发布错误消息 –
单击b5时是否出现错误? – Blackbelt