自己写的代码 ,一进虚拟机就闪退,求教。很多天了都没搞定,急求!!!!
package com.hl.mycaculater;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
public class MainActivity extends Activity {
private EditText result;//设置输出文本
private static String Num;//设置结果
private static String Num1;
private static String Num2;
private static int flag = 0;//标志位
public oprationType OT = null;
private int[] btOT = {R.id.btAdd,R.id.btSub,R.id.btMul,R.id.btDiv};
private Button[] buttonOT = new Button[btOT.length];//创建四个操作按键
private int[] btID = {R.id.bt0,R.id.bt1,R.id.bt2,R.id.bt3,R.id.bt4,
R.id.bt5,R.id.bt6,R.id.bt7,R.id.bt8,R.id.bt9,R.id.btPoi};
private Button[] BTID = new Button[btID.length];//创建数字按键
private GridLayout gly;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gly = (GridLayout) findViewById(R.layout.activity_main);
result = (EditText) findViewById(R.id.editText1);
result.setText("0");
result.setEnabled(false);
GetNumber get = new GetNumber();
for(int i=0;i<btID.length-1;i++){
BTID[i] = (Button)findViewById(btID.length);
BTID[i].setOnClickListener(get);
}
GetOpType getOT = new GetOpType();
for(int i=0;i<btID.length-1;i++){
buttonOT[i] = (Button)findViewById(btOT.length);
buttonOT[i].setOnClickListener(getOT);
}
Button btEqual = (Button) findViewById(R.id.btEqu);//设置等号键
btEqual.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (flag == 0) {
Num2 = result.getText().toString();
if (OT == oprationType.Div && Num2.equals("0")) {
result.setText("0不能为被除数");
} else {
Num = OT.Values(Num1, Num2);
Num1 = Num;
Num2 = "0";
result.setText(Num);
flag = 1;
gly.setBackgroundResource(R.drawable.ic_launcher);
}
}
}
});
Button btClean = (Button)findViewById(R.id.btCle);//设置清屏键
btClean.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Button btDelete = (Button)findViewById(R.id.btDel);//设置删除键
btDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
class GetNumber implements OnClickListener {//设置输出显示
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag == 1){
Num = "0";
}
if(Num.equals("0")){
result.setText("");
Num = v.getId() == R.id.btPoi ? "0":"";//????
}
String txt = ((Button) v).getText().toString();
boolean s = Pattern.matches("-*(\\d+).?(\\d)*", Num + txt);
Num = s ? (Num + txt) : Num;
gly.setBackgroundResource(R.drawable.ic_launcher);
result.setText(Num);
}
}
class GetOpType implements OnClickListener{//设置操作跳转
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Num1 = result.getText().toString();
switch (v.getId()) {
case R.id.btAdd:
OT = oprationType.Add;
break;
case R.id.btSub:
OT = oprationType.Sub;
break;
case R.id.btMul:
OT = oprationType.Mul;
break;
case R.id.btDiv:
OT = oprationType.Div;
break;
default:
break;
}
Num = "0";
flag = 0;
gly.setBackgroundResource(R.drawable.ic_launcher);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}