引起:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-2位于

时间:2018-04-28 18:02:14

标签: java android sqlite android-sqlite

在总活动java类

中创建了

错误

这里是logcat

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.prudentmubasha.ainnaresto / com.example.prudentmubasha.ainnaresto.TotalActivity}:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-2       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2892)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)       在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)       在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101)       在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1786)       在android.os.Handler.dispatchMessage(Handler.java:106)       在android.os.Looper.loop(Looper.java:164)       在android.app.ActivityThread.main(ActivityThread.java:6656)       at java.lang.reflect.Method.invoke(Native Method)       在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)    引起:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-2       at java.lang.String.substring(String.java:2036)       at com.example.prudentmubasha.ainnaresto.TotalActivity.onCreate(TotalActivity.java:33)       在android.app.Activity.performCreate(Activity.java:7117)       在android.app.Activity.performCreate(Activity.java:7108)       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1262)       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2867)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)       在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)       在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101)       在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1786)       在android.os.Handler.dispatchMessage(Handler.java:106)       在android.os.Looper.loop(Looper.java:164)       在android.app.ActivityThread.main(ActivityThread.java:6656)       at java.lang.reflect.Method.invoke(Native Method)       在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

这是发生错误的地方

enter c while (st.hasMoreElements()) {
    //Toast.makeText(getApplicationContext(), st.nextElement().toString().substring(0,1), Toast.LENGTH_LONG).show();
    test = st.nextElement().toString().substring(3,1);
    if(count>0)
        total+= Float.parseFloat(test);
    count++;
}

这是总活动java类文件

 mApp=((pizza) getApplicationContext());
et_summary = (EditText) findViewById(R.id.et_summary);
tv_total = (TextView) findViewById(R.id.tv_total);
//Toast.makeText(getApplicationContext(), "You have chosen the" + " " +  mApp.getGlobalVarValue(), Toast.LENGTH_LONG).show();
et_summary.setText(mApp.getGlobalVarValue());
String str = mApp.getGlobalVarValue();
StringTokenizer st = new StringTokenizer(str,"$");
String test="";
float total=0;
int count=0;
while (st.hasMoreElements()) {
    //Toast.makeText(getApplicationContext(), st.nextElement().toString().substring(0,1), Toast.LENGTH_LONG).show();
    test = st.nextElement().toString().substring(3,1);
    if(count>0)
        total+= Float.parseFloat(test);
    count++;
}
et_summary.setText(mApp.getGlobalVarValue());
tv_total.setText("Total :" + total+"");
mApp.setGlobalClear();


}
public boolean isFloat( String input )
{
try
{
    Float.parseFloat( input );
    return true;
}
catch( Exception e)
{
    return false;
}
}
}

1 个答案:

答案 0 :(得分:2)

你不能从3到1子串 交换

substring(3,1);

代表

substring(1,3)