Je suis nouveau sur Stack Overflow, je suis impatient de vous aider.
Je suis nouveau dans la programmation et le développement de logiciels, en particulier le développement Android.java.lang.NumberFormatException StopWatch.roundTwoDecimals
J'ai développé une application mobile et je reçois cette erreur à partir des rapports d'erreur dans le Play Store.
java.lang.NumberFormatException
com.mt.applicaiton.StopWatch.roundTwoDecimals
Pourriez-vous nous aider s'il vous plaît?
Voici le code:
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));
}
}
Et voici le 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)
ce qui est 'roundTwoDecimals'? si la méthode que où est la déclaration pour elle? –