我在Android中制作了一个简单的计算器应用程序,但是我遇到了一些错误,当我按下百分比按钮时,应用程序崩溃并给我以下错误。基本上我要做的是,如果任何符号出现在字符串的最后一个索引上,EditText
值应该递减1并且将分配新值。在按下百分比按钮之前按下任何其他按钮时,它工作正常。但是,如果我先按下百分比按钮,那么应用程序的其他按钮会崩溃。这基本上就是问题所在。为了避免首先放置符号,我写了最后一个else块,它只是在Toast
中显示一条消息,但现在崩溃了。
以下是Java代码
public void calculate(View v){
String t;
ma_res_txt = (TextView) findViewById(R.id.ma_res_txt);
String[] signs ={"%","÷","+","×","π","!","√",".","×^"};
int length = ma_res_txt.getText().length();
String txt = ma_res_txt.getText().toString();
switch (v.getId()){
case R.id.cal_btn_clear:{
ma_res_txt.setText("");
break;
}
case R.id.cal_btn_del:{
try {
if(length>0){
ma_res_txt.setText(txt.substring(0, length - 1));
} else{
Toast.makeText(MainActivity.this,"No more characters left",Toast.LENGTH_LONG).show();
}
}catch (Exception e){
Toast.makeText(MainActivity.this,"No more characters left",Toast.LENGTH_LONG).show();
}
break;
}
case R.id.cal_btn_percent:{
t = "%";
String s = txt.substring(length-1,length);
if(length>0) {
if(s.equals("%") || s.equals("÷") || s.equals("+") || s.equals("×") || s.equals("^") ){
ma_res_txt.setText(txt.substring(0, length - 1));
ma_res_txt.append(t);
}
else {
ma_res_txt.append(t);
}
}
else{
Toast.makeText(MainActivity.this,"Syntax Error !",Toast.LENGTH_LONG).show();
}
break;
}
}
}
这是我得到的错误。即使周围有try catch块,应用程序仍会崩溃。
08-02 20:17:16.105 26307-26307/com.example.nadeemahmad.smartcalculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nadeemahmad.smartcalculator, PID: 26307
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5044)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5039)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.StringIndexOutOfBoundsException: length=0; index=-1
at java.lang.String.substring(String.java:1926)
at com.example.nadeemahmad.smartcalculator.MainActivity.calculate(MainActivity.java:87)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5039)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
XML
<EditText
android:layout_width="match_parent"
android:layout_height="211dp"
android:background="#fff"
android:id="@+id/ma_res_txt"
android:paddingLeft="17dp"
android:textSize="30dp"
android:inputType="none"
android:focusedByDefault="true"
android:focusableInTouchMode="true"
android:textIsSelectable="true"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/btn_ctrl_linear"
android:layout_below="@+id/ma_res_txt"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Voice Control"
android:layout_weight="1"
android:id="@+id/show_voice_ctrl"
android:background="@drawable/custom_btn_ripple_blue_white"
android:paddingLeft="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camera Control"
android:layout_weight="1"
android:id="@+id/show_cam_ctrl"
android:background="@drawable/custom_btn_ripple_blue_white"
android:paddingLeft="10dp"
/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_ctrl_linear"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/cal_btn_set1"
>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="C"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:textColor="#027fcc"
android:id="@+id/cal_btn_clear"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="%"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:textColor="#027fcc"
android:id="@+id/cal_btn_percent"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="÷"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textColor="#027fcc"
android:textSize="20dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_divide"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="del"
android:textAllCaps="false"
android:textColor="#027fcc"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_del"
android:onClick="calculate"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/cal_btn_set2"
android:layout_below="@+id/cal_btn_set1"
>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="7"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_7"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="8"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_8"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="9"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_9"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="+"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="20dp"
android:textColor="#027fcc"
android:layout_margin="1dp"
android:id="@+id/cal_btn_plus"
android:onClick="calculate"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/cal_btn_set3"
android:layout_below="@+id/cal_btn_set2"
>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="4"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_4"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="5"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_5"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="6"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_6"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="×"
android:typeface="monospace"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="24dp"
android:textColor="#027fcc"
android:layout_margin="1dp"
android:id="@+id/cal_btn_mult"
android:onClick="calculate"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/cal_btn_set4"
android:layout_below="@+id/cal_btn_set3"
>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="1"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_1"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="2"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_2"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="3"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_3"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="π"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="16dp"
android:textColor="#027fcc"
android:layout_margin="1dp"
android:id="@+id/cal_btn_pi"
android:onClick="calculate"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/cal_btn_set5"
android:layout_below="@+id/cal_btn_set4"
>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="√"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="18dp"
android:textColor="#027fcc"
android:layout_margin="1dp"
android:id="@+id/cal_btn_root"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="0"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="18dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_0"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="."
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="24dp"
android:textColor="#027fcc"
android:layout_margin="1dp"
android:id="@+id/cal_btn_dot"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="×^"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="18dp"
android:textColor="#027fcc"
android:layout_margin="1dp"
android:id="@+id/cal_btn_power"
android:onClick="calculate"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/cal_btn_set6"
android:layout_below="@+id/cal_btn_set5"
>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="sin"
android:textAllCaps="false"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="14dp"
android:textColor="#027fcc"
android:textStyle="italic"
android:layout_margin="1dp"
android:id="@+id/cal_btn_sin"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="cos"
android:textAllCaps="false"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="14dp"
android:textColor="#027fcc"
android:textStyle="italic"
android:layout_margin="1dp"
android:id="@+id/cal_btn_cos"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="tan"
android:textAllCaps="false"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="14dp"
android:textStyle="italic"
android:textColor="#027fcc"
android:layout_margin="1dp"
android:id="@+id/cal_btn_tan"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="!"
android:layout_weight="1"
android:textColor="#027fcc"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="20dp"
android:layout_margin="1dp"
android:id="@+id/cal_btn_ptrl"
android:onClick="calculate"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/cal_btn_set7"
android:layout_below="@+id/cal_btn_set6"
>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="sin(-1)"
android:textAllCaps="false"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="12dp"
android:textColor="#027fcc"
android:textStyle="italic"
android:layout_margin="1dp"
android:id="@+id/cal_btn_sinIn"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="cos(-1)"
android:textAllCaps="false"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="12dp"
android:textColor="#027fcc"
android:textStyle="italic"
android:layout_margin="1dp"
android:id="@+id/cal_btn_cosIn"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="tan(-1)"
android:textAllCaps="false"
android:layout_weight="1"
android:background="@drawable/custom_btn_ripple_blue"
android:textSize="12dp"
android:textStyle="italic"
android:textColor="#027fcc"
android:layout_margin="1dp"
android:id="@+id/cal_btn_tanIn"
android:onClick="calculate"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="65dp"
android:text="="
android:layout_weight="1"
android:background="@drawable/custome_btn_ripple_grey"
android:textSize="30dp"
android:textColor="#fff"
android:layout_margin="1dp"
android:id="@+id/cal_btn_equal"
android:onClick="calculate"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
答案 0 :(得分:1)
当你执行String s = txt.substring(length-1,length);
时,你还没有检查你的长度&gt;如果您先按%按钮,那么您的txt为空,因此您的索引超出范围