簡単な電卓を作ろうとコードを書いてみたのですが、エミュレーターが動かない!! 「申し訳ありませんが、アプリケーション Task_6 (プロセス com.example.task_6 ) が予期せず停止しました。もう一度やり直してください。」というメッセージが表示されました。
これは LogCat からのメッセージです: "at dalvik.system.NativeStart.main(Native method)" ボタン (+) またはバックスペース ボタンをクリックしたとき:\\
レイアウトは windows 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;
}
}