我无法在代码中找到错误当我按“CE”时,应用程序停止工作请提供一些解决方案,以及如何调整它以使其适合我的屏幕
TestActivity.java
public class TestActivity extends Activity {
private EditText text;
String operators[]={"sin","cos","tan","asin","acos",
"atan","^2","^3","^-1","sqrt",
"cuberoot","log","exp","x!","^n"} ;
String operators1[]= {"A","B","C","D","E","F", "G","H","I","J","K","L","M","N","O"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.editText1);
text.setEnabled(false);
}
public void myClickHandler(View view) {
String s1,s2 = null,s3,s4,s5;
switch (view.getId()) {
case R.id.button1:
s3=text.getText().toString();
s4="+";
s5=s3+s4;
s2=s5;
text.setText(s5);
break;
case R.id.button2:
s3=text.getText().toString();
s4="-";
s5=s3+s4;
s2=s5;
text.setText(s5);
break;
case R.id.button3
:s3=text.getText().toString();
s4="*";
s5=s3+s4;
s2=s5;
text.setText(s5);
break;
case R.id.button4:
s3=text.getText().toString();
s4="/";
s5=s3+s4;
s2=s5;
text.setText(s5);
break;
case R.id.button5:
s3=text.getText().toString();
int a=s3.length();
int b=s3.length();
String x=s2.substring(a-2,a-1);
if(x.equals("A"))
{s4=s3.substring(0,(a-5));
s2=s2.substring(0,b-1);
}
else if(x.equals("B"))
{s4=s3.substring(0,(a-5));
s2=s2.substring(0,b-1);
}
else if(x.equals("C"))
{s4=s3.substring(0,(a-5));
s2=s2.substring(0,b-1);
}
else if(x.equals("D"))
{s4=s3.substring(0,(a-6));
s2=s2.substring(0,b-1);
}
else if(x.equals("E"))
{s4=s3.substring(0,(a-6));
s2=s2.substring(0,b-1);
}
else if(x.equals("F"))
{s4=s3.substring(0,(a-6));
s2=s2.substring(0,b-1);
}
else if(x.equals("G"))
{s4=s3.substring(0,(a-3));
s2=s2.substring(0,b-1);
}
else if(x.equals("H"))
{s4=s3.substring(0,(a-3));
s2=s2.substring(0,b-1);
}
else if(x.equals("I"))
{s4=s3.substring(0,(a-4));
s2=s2.substring(0,b-1);
}
else if(x.equals("J"))
{s4=s3.substring(0,(a-6));
s2=s2.substring(0,b-1);
}
else if(x.equals("K"))
{s4=s3.substring(0,(a-10));
s2=s2.substring(0,b-1);
}
else
{
s4=s3.substring(0,(a-1));
s2=s2.substring(0,b-1);
}
s5=s4;
text.setText(s5);
break;
case R.id.button6:
s3=text.getText().toString();
s2=s3;
if (text.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;}
break;
case R.id.button7:
s3=text.getText().toString();
s4="(";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button8:
s3=text.getText().toString();
s4=")";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button9:
s3=text.getText().toString();
s4="1";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button10:
s3=text.getText().toString();
s4="2";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button11:
s3=text.getText().toString();
s4="3";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button12:
s3=text.getText().toString();
s4="4";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button13:
s3=text.getText().toString();
s4="5";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button14:
s3=text.getText().toString();
s4="6";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button15:
s3=text.getText().toString();
s4="7";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button16:
s3=text.getText().toString();
s4="8";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button17:
s3=text.getText().toString();
s4="9";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button18:
s3=text.getText().toString();
s4="0";
s2=s2+s4;
s5=s3+s4;
text.setText(s5);
break;
case R.id.button19:
s3=text.getText().toString();
s4="sin(";
s2=s2+"A";
s5=s3+s4;
text.setText(s5);
break;
case R.id.button20:
s3=text.getText().toString();
s4="cos(";
s2=s2+"B";
s5=s3+s4;
text.setText(s5);
break;
case R.id.button21:
s3=text.getText().toString();
s4="tan(";
s2=s2+"C";
s5=s3+s4;
text.setText(s5);
break;
case R.id.button22:
s3=text.getText().toString();
s4="asin(";
s2=s2+"D";
s5=s3+s4;
text.setText(s5);
break;
case R.id.button23:
s3=text.getText().toString();
s4="acos(";
s2=s2+"E";
s5=s3+s4;
text.setText(s5);
break;
case R.id.button24:
s3=text.getText().toString();
s4="atan(";
s2=s2+"F";
s5=s3+s4;
text.setText(s5);
break;
case R.id.button25:
s3=text.getText().toString();
s2=s2+"G";
s4="^2";
s5=s3+s4;
text.setText(s5);
break;
case R.id.button26:
s3=text.getText().toString();
s4="^3";
s2=s2+"H";
s5=s3+s4;
text.setText(s5);
break;
case R.id.button27:
s3=text.getText().toString();
s4="^-1";
s2=s2+"I";
s5=s3+s4;
text.setText(s5);
break;
case R.id.button28:
s3=text.getText().toString();
s4="sqrt(";
s5=s3+s4;
s2=s2+"J";
text.setText(s5);
break;
case R.id.button29:
s3=text.getText().toString();
s4="cuberoot(";
s5=s3+s4;
s2=s2+"K";
text.setText(s5);
break;
case R.id.button30:
s3=text.getText().toString();
s4=".";
s5=s3+s4;
text.setText(s5);
break;
case R.id.button31:
System.exit(0);
break;
}
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPostalAddress" >
</EditText>
</TableRow>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="myClickHandler"
android:text="@string/add" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="myClickHandler"
android:text="@string/sub" />
/>
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="myClickHandler"
android:text="@string/mul" />
/>
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="myClickHandler"
android:text="@string/div" />
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="myClickHandler"
android:text="@string/clear" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/square" />
<Button
android:id="@+id/button26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/cube" />
<Button
android:id="@+id/button28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/squareroot" />
<Button
android:id="@+id/button29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/cuberoot" />
<Button
android:id="@+id/button27"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/inverse" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/sin" />
<Button
android:id="@+id/button15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/seven" />
<Button
android:id="@+id/button16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/eight" />
<Button
android:id="@+id/button17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/nine" />
<Button
android:id="@+id/button22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/asin" />
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/cos" />
<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/four" />
<Button
android:id="@+id/button13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/five" />
<Button
android:id="@+id/button14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/six" />
<Button
android:id="@+id/button23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/acos" />
</TableRow>
<TableRow
android:id="@+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/tan" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/one" />
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/two" />
<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/three" />
<Button
android:id="@+id/button24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/atan" />
</TableRow>
<TableRow
android:id="@+id/tableRow8"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/equal" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/openbar" />
<Button
android:id="@+id/button18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/zero" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/closebar" />
<Button
android:id="@+id/button30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/dot" />
</TableRow>
</TableLayout>
<TableRow
android:id="@+id/tableRow9"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button31"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/exit" />
</TableRow>
</LinearLayout>