2017-08-30 31 views
-2

我是Stack Overflow的新手,我很期待你們的幫助。
我是編程和軟件開發新手,尤其是Android開發人員。java.lang.NumberFormatException StopWatch.roundTwoDecimals

我開發了一個移動應用程序,並且在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) 
+0

是什麼'roundTwoDecimals'?如果方法比在哪裏聲明它呢? –

回答

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); 
+0

這裏是崩潰日誌 –

+0

@MuhammadAzeemIqbal:這應該是在問題,而不是一個答案 - 你似乎已經忽略了我的答案的所有內容... –

相關問題