2014-12-01 5 views
0

이것을 실행할 때이 숫자 형식 오류가 발생합니다. 그것은 단지 내가 견해를 표시하고 오른쪽 속도를 계산하거나하지 않을 경우 테스트 할 OnLocationChanged 방법에 줄을 주석 때 발생하는 것, 어떤 도움이 많이 주시면 감사하겠습니다 :)스톱워치 오류가 발생했습니다. - java.lang.NumberFormatException : 유효하지 않은 int : "0:00"

package com.nathan.trackrun; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesClient; 
import com.google.android.gms.location.LocationClient; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 

import android.content.pm.ActivityInfo; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.Chronometer; 
import android.widget.CompoundButton; 
import android.widget.TextView; 
import android.widget.ToggleButton; 

/** 
* A simple {@link android.support.v4.app.Fragment} subclass. 
* 
*/ 

public class FragmentStats extends Fragment implements 
     GooglePlayServicesClient.ConnectionCallbacks, 
     GooglePlayServicesClient.OnConnectionFailedListener, LocationListener { 

    public FragmentStats() { 
     // Required empty public constructor 
    } 

    LocationManager statsManager; 
    TextView speedView; 
    TextView speedUnit; 
    TextView unitView; 
    TextView timerView; 
    TextView distanceView; 
    TextView distanceUnit; 
    ToggleButton buttonStart; 
    Button buttonSave; 
    Button buttonReset; 
    ToggleButton buttonSpeedUnit; 
    String mySpeedString; 
    String kmhString = "Km/h"; 
    boolean checkSpeedUnit; 
    private LocationRequest statsLocationRequest; 
    private LocationClient statsLocationClient; 
    FragmentMap fMap; 
    Chronometer stopwatch; 
    String chronoText; 
    boolean reset; 

    public static boolean startStop; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     // Inflate the layout for this fragment 

     View viewStats = inflater.inflate(R.layout.fragment_stats, container, 
       false); 

     // Reference buttons from XML into Java 
     buttonStart = (ToggleButton) viewStats 
       .findViewById(R.id.stats_start_button); 
     buttonSave = (Button) viewStats.findViewById(R.id.save_button); 
     buttonReset = (Button) viewStats.findViewById(R.id.reset_button); 
     buttonSpeedUnit = (ToggleButton) viewStats 
       .findViewById(R.id.speed_unit_button); 
     speedView = (TextView) viewStats.findViewById(R.id.speed_int); 
     speedUnit = (TextView) viewStats.findViewById(R.id.speed_unit); 
     stopwatch = (Chronometer) viewStats.findViewById(R.id.stopwatchView); 
     distanceUnit = (TextView) viewStats.findViewById(R.id.disctance_unit); 
     distanceView = (TextView) viewStats.findViewById(R.id.distance_int); 

     /* 
     * buttonReset.setOnClickListener(new View.OnClickListener() { 
     * 
     * @Override public void onClick(View v) { // TODO Auto-generated method 
     * stub 
     * 
     * pausedMilliseconds = 0; 
     * 
     * stopwatch.setBase(SystemClock.elapsedRealtime()); reset = true; 
     * 
     * } }); 
     */ 

     buttonStart 
       .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(CompoundButton buttonView, 
          boolean isChecked) { 
         // TODO Auto-generated method stub 

         reset = false; 
         if (isChecked) { 

          int pausedMilliseconds; 
          pausedMilliseconds = 0; 
          String chronoText = stopwatch.getText().toString(); 
          String chronoArray[] = chronoText.split(":"); 

          if (chronoArray.length == 2) { 

           pausedMilliseconds = Integer 
             .parseInt(chronoArray[0]) 
             * 60 
             * 1000 
             + Integer.parseInt(chronoArray[1]) 
             * 1000; 

          } else if (chronoArray.length == 3) { 

           pausedMilliseconds = Integer 
             .parseInt(chronoArray[0]) 
             * 60 
             * 60 
             * 1000 
             + Integer.parseInt(chronoArray[1]) 
             * 60 
             * 1000 
             + Integer.parseInt(chronoArray[2]) 
             * 1000; 
          } 

          stopwatch.setBase(SystemClock.elapsedRealtime() 
            - pausedMilliseconds); 
          stopwatch.start(); 

         } 

         else { 

          stopwatch.stop(); 
         } 

        } 
       }); 

     buttonSpeedUnit 
       .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(CompoundButton buttonView, 
          boolean isChecked) { 
         // TODO Auto-generated method stub 

         if (isChecked == true) { 
          checkSpeedUnit = true; 
          speedUnit.setText("Km/h"); 
          distanceUnit.setText("KM"); 

         } else { 
          checkSpeedUnit = false; 
          speedUnit.setText("Mph"); 
          distanceUnit.setText("Miles"); 
         } 

        } 
       }); 

     buttonSave.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

      } 
     }); 

     return viewStats; 

    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     statsLocationRequest = LocationRequest.create(); 
     statsLocationRequest 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     // Set the update interval to 5 seconds 
     statsLocationRequest.setInterval(5000); 
     // Set the fastest update interval to 1 second 
     statsLocationRequest.setFastestInterval(1000); 
     statsLocationClient = new LocationClient(this.getActivity() 
       .getApplicationContext(), this, this); 
     statsLocationClient.connect(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

     //Below line commented out and replaced with mySpeed dummy value to see if it works 
     // if (location.hasSpeed()) { 
     float mySpeed = location.getSpeed(); 
     mySpeed = (float) 5.5; 
     float distanceUpdater = Integer.parseInt((String) stopwatch.getText()) 
       * mySpeed; 
     float distance = 0; 
     String distanceString; 
     distance = distance + distanceUpdater; 

     if (checkSpeedUnit == true) { 

      mySpeed = (float) (mySpeed * 3.6); 
      distance = (float) (distance/1000); 
      distanceString = Float.toString(distance); 
      mySpeed = 1; 
      distanceView.setText(distanceString); 
      mySpeedString = Float.toString(mySpeed); 
      speedView.setText(mySpeedString); 
     } 

     if (checkSpeedUnit == false) { 

      mySpeed = (float) (mySpeed * 2.237); 
      distance = (float) (distance * 0.000621371192); 
      distanceString = Float.toString(distance); 
      mySpeed = 1; 
      distanceView.setText(distanceString); 
      mySpeedString = Float.toString(mySpeed); 
      speedView.setText(mySpeedString); 

     } 

    } 

    // } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     // TODO Auto-generated method stub 
     statsLocationClient.requestLocationUpdates(statsLocationRequest, this); 

    } 

    @Override 
    public void onDisconnected() { 
     // TODO Auto-generated method stub 

    } 

} 

로그 캣 :

12-01 20:59:19.715: D/ActivityThread(29203): handleBindApplication:com.nathan.trackrun 
12-01 20:59:19.715: D/ActivityThread(29203): setTargetHeapUtilization:0.75 
12-01 20:59:19.715: D/ActivityThread(29203): setTargetHeapMinFree:2097152 
12-01 20:59:19.875: I/u(29203): Making Creator dynamically 
12-01 20:59:19.925: I/Google Maps Android API(29203): Google Play services client version: 4452000 
12-01 20:59:19.925: I/Google Maps Android API(29203): Google Play services package version: 6587038 
12-01 20:59:20.505: W/ActivityThread(29203): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader()); 
12-01 20:59:20.685: I/Adreno-EGL(29203): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LNX.LA.3.5.2.2_RB1.04.04.04.087.028_msm8974_LNX.LA.3.5.2.2_RB1__release_AU() 
12-01 20:59:20.685: I/Adreno-EGL(29203): OpenGL ES Shader Compiler Version: E031.24.00.15 
12-01 20:59:20.685: I/Adreno-EGL(29203): Build Date: 07/31/14 Thu 
12-01 20:59:20.685: I/Adreno-EGL(29203): Local Branch: 
12-01 20:59:20.685: I/Adreno-EGL(29203): Remote Branch: quic/LNX.LA.3.5.2.2_rb1 
12-01 20:59:20.685: I/Adreno-EGL(29203): Local Patches: NONE 
12-01 20:59:20.685: I/Adreno-EGL(29203): Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.5.2.2_RB1.04.04.04.087.028 + NOTHING 
12-01 20:59:20.705: D/OpenGLRenderer(29203): Enabling debug mode 0 
12-01 20:59:20.915: I/Timeline(29203): Timeline: Activity_idle id: [email protected] time:109078101 
12-01 20:59:26.185: D/AndroidRuntime(29203): Shutting down VM 
12-01 20:59:26.185: W/dalvikvm(29203): threadid=1: thread exiting with uncaught exception (group=0x415ffdb8) 
12-01 20:59:26.195: E/AndroidRuntime(29203): FATAL EXCEPTION: main 
12-01 20:59:26.195: E/AndroidRuntime(29203): Process: com.nathan.trackrun, PID: 29203 
12-01 20:59:26.195: E/AndroidRuntime(29203): java.lang.NumberFormatException: Invalid int: "0:00" 
12-01 20:59:26.195: E/AndroidRuntime(29203): at java.lang.Integer.invalidInt(Integer.java:137) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at java.lang.Integer.parse(Integer.java:374) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at java.lang.Integer.parseInt(Integer.java:365) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at java.lang.Integer.parseInt(Integer.java:331) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at com.nathan.trackrun.FragmentStats.onLocationChanged(FragmentStats.java:205) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at com.google.android.gms.internal.hb$a.handleMessage(Unknown Source) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at android.os.Handler.dispatchMessage(Handler.java:102) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at android.os.Looper.loop(Looper.java:136) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at android.app.ActivityThread.main(ActivityThread.java:5146) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at java.lang.reflect.Method.invokeNative(Native Method) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at java.lang.reflect.Method.invoke(Method.java:515) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) 
12-01 20:59:26.195: E/AndroidRuntime(29203): at dalvik.system.NativeStart.main(Native Method) 

답변

2

은 음, 여기

float distanceUpdater = Integer.parseInt((String) stopwatch.getText()) * mySpeed; 

코드는 : 문자 문자열 데이터가 될 것입니다. 함께 작업하는 데이터를 구문 분석해야합니다.

String[] parsedMinutesAndSeconds = stopWatch.getText().split(":"); 
String minutes = parsedMinutesAndSeconds[0]; 
String seconds = parsedMinutesAndSeconds[1]; 

그리고이 두 필드의 정수를 구문 분석하여 처리 할 수 ​​있습니다. 2 시간 또는 3 시간 동안의 필드가 있는지 또는 시계가 작동하는 방식에 따라 사용 가능한 시간이 없는지 여부를 확인하는 것이 좋습니다.

+0

오, 이런! 건배 야 젠장, 지금은 XD 덕분에이 앱을 단단히 작업하고있다. 그물을 통과 할 수 있다고 생각한다.) –

+0

방금 ​​코드를 작성 했으므로 대우를 받았습니다. 감사합니다. :) –

관련 문제