秒表错误,帮助!! - java.lang.NumberFormatException:无效的int:“0:00”

时间:2014-12-01 21:02:41

标签: java android eclipse

当我运行此操作时,我收到此数字格式错误。只有在我注释掉OnLocationChanged方法中的行来测试视图是否会显示并计算出合适的速度时,才会发生这种情况,任何帮助都会非常感激:)

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

    }

}

logcat的:

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: android.os.BinderProxy@418b7b90 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)

1 个答案:

答案 0 :(得分:2)

嗯,你的代码在这里

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

将成为具有:字符的字符串数据。您必须解析您正在使用的数据。

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

然后你可以解析这两个字段的整数并使用它们。根据时钟的工作原理,值得检查是否有2个或3个字段可用数小时或没有可用时间。