java.lang.NumberFormatException StopWatch.roundTwoDecimals

时间:2017-08-30 06:08:46

标签: java android numberformatexception

我是Stack Overflow的新手,我很期待你们帮助我 我是编程和软件开发的新手,特别是Android开发。

我开发了一个移动应用程序,我从Play商店的崩溃报告中收到此错误。

java.lang.NumberFormatException
com.mt.applicaiton.StopWatch.roundTwoDecimals

有人可以帮忙吗?

以下是代码:

public class StopWatch extends ActionBarActivity {
    Button butnstart, butnreset,btnstop;
    TextView time;
    long starttime = 0L;
    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedtime = 0L;
    int t = 1;
    int secs = 0;
    int mins = 0;
    int milliseconds = 0;
    Handler handler = new Handler();
    SharedPreferences stppreferences;
    SharedPreferences.Editor editor1;
    int stpvalue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stop_watch);
        butnstart = (Button) findViewById(R.id.start);
        butnreset = (Button) findViewById(R.id.reset);
        btnstop = (Button) findViewById(R.id.stop);
        time = (TextView) findViewById(R.id.timer);

        stppreferences = PreferenceManager.getDefaultSharedPreferences(StopWatch.this);


        butnstart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                if (t == 1) {
                    butnstart.setText("Pause");
                    starttime = SystemClock.uptimeMillis();
                    handler.postDelayed(updateTimer, 0);
                    t = 0;
                } else {
                    butnstart.setText("Start");
                    time.setTextColor(Color.BLUE);
                    timeSwapBuff += timeInMilliseconds;
                    handler.removeCallbacks(updateTimer);
                    t = 1;
               }

            }
        });

        btnstop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                    String text = time.getText().toString();
                    String[] timelist = text.split(":");
                    String strmin = timelist[0];
                    String strsecond = timelist[1];
                    String strmilisecond = timelist[2];
                    double min =Double.parseDouble(strmin);
                    double fmin= min * 60;
                    Float mili = Float.parseFloat(strmilisecond);
                    double convermili= mili/1000.0;

                    double result= fmin+ Double.parseDouble(strsecond)+convermili ;
                    double myresult= roundTwoDecimals(result);
                    String strresult= Double.toString(myresult);


                  // Setting name of button in collect sample
                SharedPreferences settings = getSharedPreferences("watchdynamicbtname", 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("watchbtnname",strresult);
                editor.commit();
                // Getting value from CollectSample Click for setting particular name
                stppreferences = PreferenceManager.getDefaultSharedPreferences(StopWatch.this);
                stpvalue= stppreferences.getInt("stpvalue",0);
                int x= stpvalue;
               // calling particular button name CollectSample.OnResume
                editor1 = PreferenceManager.getDefaultSharedPreferences(StopWatch.this).edit();
                editor1.putInt("stpvalue",x).apply();
                starttime = SystemClock.uptimeMillis();
                handler.postDelayed(updateTimer, 0);
                handler.removeCallbacks(updateTimer);
                finish();

            }
        });




        butnreset.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {

                starttime = 0L;
                timeInMilliseconds = 0L;
                timeSwapBuff = 0L;
                updatedtime = 0L;
                t = 1;
                secs = 0;
                mins = 0;
                milliseconds = 0;
                butnstart.setText("Start");
                handler.removeCallbacks(updateTimer);
                time.setText("00:00:00");

            }
        });

    }

    public Runnable updateTimer = new Runnable() {

        public void run() {

            timeInMilliseconds = SystemClock.uptimeMillis() - starttime;

            updatedtime = timeSwapBuff + timeInMilliseconds;

            secs = (int) (updatedtime / 1000);
            mins = secs / 60;
            secs = secs % 60;
            milliseconds = (int) (updatedtime % 1000);
            time.setText("" + mins + ":" + String.format("%02d", secs) + ":"
                    + String.format("%03d", milliseconds));
            time.setTextColor(Color.RED);
            handler.postDelayed(this, 0);
        }

    };
    roundTwoDecimals(double d) {
    DecimalFormat twoDForm = new DecimalFormat("#.##");
    return Double.valueOf(twoDForm.format(d));
}
}

这是Logcat:

java.lang.NumberFormatException: 
  at java.lang.StringToReal.invalidReal (StringToReal.java:63)
  at java.lang.StringToReal.initialParse (StringToReal.java:164)
  at java.lang.StringToReal.parseDouble (StringToReal.java:282)
  at java.lang.Double.parseDouble (Double.java:301)
  at java.lang.Double.valueOf (Double.java:338)
  at com.mt.puzzlesolutions.free.version.StopWatch.roundTwoDecimals (StopWatch.java:168)
  at com.mt.puzzlesolutions.free.version.StopWatch$2.onClick (StopWatch.java:86)
  at android.view.View.performClick (View.java:5207)
  at android.view.View$PerformClick.run (View.java:21177)
  at android.os.Handler.handleCallback (Handler.java:739)
  at android.os.Handler.dispatchMessage (Handler.java:95)
  at android.os.Looper.loop (Looper.java:148)
  at android.app.ActivityThread.main (ActivityThread.java:5458)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:738)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:628)

1 个答案:

答案 0 :(得分:3)

Double.valueOf期望使用点作为小数点格式化的值。您正在创建使用系统默认语言环境的DecimalFormat,而.则是特定于语言环境的小数分隔符。

可能起作用的最小变化是:

DecimalFormat twoDForm = new DecimalFormat(
    "#.##", DecimalFormatSymbols.getInstance(Locale.US));

这将始终使用美国符号,其中有一个点作为小数分隔符。

但是,您应该期望您仍然没有double 正好两位小数的值。例如,0.01没有精确的double表示,因此您最终会得到“最接近0.01的双倍”。

鉴于当您调用roundTwoDecimals然后立即格式化字符串时,您可能只想更改它以返回字符串(并相应地重命名):

String formatTo2DP(double d) {
    DecimalFormat twoDForm = new DecimalFormat(
        "#.##", DecimalFormatSymbols.getInstance(Locale.US));
    return twoDForm.format(d);
}

格式化,然后解析,然后重新格式化没有意义。

然后更改此调用代码:

double myresult= roundTwoDecimals(result);
String strresult= Double.toString(myresult);

只是:

String formattedResult = formatTo2DP(result);