错误NumberFormatException Android

时间:2015-09-12 15:21:48

标签: android gps runtime-error

点击我的操作按钮后出错。我从一个名为"的类中取两个坐标。 GPSTracker"然后我用方法回到我的班级#34; getLatitude"和" getLongitude" 。如果我点击我的操作按钮,我会收到此错误:

09-12 17:07:46.898  24575-24575/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.application.marcoopsone.centrocommercialecampania, PID: 24575
java.lang.NumberFormatException: Invalid double: "4782877,28"
        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 com.application.marcoopsone.centrocommercialecampania.FragmentCar.getFila(FragmentCar.java:177)
        at com.application.marcoopsone.centrocommercialecampania.FragmentCar$1.onClick(FragmentCar.java:95)
        at android.view.View.performClick(View.java:5254)
        at android.view.View$PerformClick.run(View.java:21174)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6862)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

如果我在模拟器genymotion上运行应用程序,那好吧!如果我使用我的三星平板电脑,我会收到错误消息。我该如何解决?

1 个答案:

答案 0 :(得分:0)

<强> GPSTracker

导入...

公共类GPSTracker扩展服务实现LocationListener {

private final Context context;

boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location;

double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.context = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {

        } else {
            this.canGetLocation = true;

            if (isNetworkEnabled) {

                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                    if (location != null) {

                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }

            }

            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}


public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

public double getLatitude() {
    if(location != null) {
        latitude = location.getLatitude();
    }
    return latitude;
}

public double getLongitude() {
    if(location != null) {
        longitude = location.getLongitude();
    }

    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

    alertDialog.setTitle("GPS is settings");

    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(intent);
        }
    });

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    alertDialog.show();
}

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

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

片段车

公共类FragmentCar扩展了android.support.v4.app.Fragment {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private TextView textFila;
private Button btn;

//VARIABILI GPS

GPSTracker gps;

static final int RAGGIO = 6371;
static final double PIGRECO = Math.PI;


double f1_latitudine = 41.00413;
double f1_longitudine = 14.32642;

double f2_latitudine = 41.00399;
double f2_longitudine = 14.32643;

double f3_latitudine = 41.00385;
double f3_longitudine = 14.32645;

double f4_latitudine = 41.0037;
double f4_longitudine = 14.32647;

double f5_latitudine = 41.00359;
double f5_longitudine = 14.32648;

double p_latitudine = 41.00388;
double p_longitudine = 14.32584;

double ps_latitudine = 41.00402;
double ps_longitudine = 14.3248;

private FloatingActionButton mFAB;


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

public static android.support.v4.app.Fragment newInstance(String param1, String param2) {
    FragmentCar fragment = new FragmentCar();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View layout = inflater.inflate(R.layout.fragment_car, container, false);

    //setupFAB();

    textFila = (TextView) layout.findViewById(R.id.textFila);

    btn = (Button) layout.findViewById(R.id.buttonSavePosition);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getFila();
        }
    });

    return layout;
}

private void setupFAB() {
    //define the icon for the main floating action button
    ImageView iconFAB = new ImageView(getActivity());
    iconFAB.setImageResource(R.drawable.ic_action_new);

    //set the appropriate background for the main floating action button along with its icon
    mFAB = new FloatingActionButton.Builder(getActivity())
            .setContentView(iconFAB)
            .setBackgroundDrawable(R.drawable.selector_button_red)
            .build();

    mFAB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getFila();
        }
    });

}

public void getFila(){

    gps = new GPSTracker(getActivity());

    if(gps.canGetLocation()) {

        double latitude = 0;
        double longitude = 0;

        latitude = new Double(gps.getLatitude());
        longitude = new Double(gps.getLongitude());

        DecimalFormat df = new DecimalFormat("0.000000");
        double lat = Double.parseDouble(df.format(latitude));
        double lon = Double.parseDouble(df.format(longitude));


        double laRadianti = (lat * PIGRECO) / 180;
        double loRadianti = (lon * PIGRECO) / 180;

        double f1_latitudineRadianti = (f1_latitudine * PIGRECO) / 180;
        double f1_longitudineRadianti = (f1_longitudine * PIGRECO) / 180;
        double f2_latitudineRadianti = (f2_latitudine * PIGRECO) / 180;
        double f2_longitudineRadianti = (f2_longitudine * PIGRECO) / 180;
        double f3_latitudineRadianti = (f3_latitudine * PIGRECO) / 180;
        double f3_longitudineRadianti = (f3_longitudine * PIGRECO) / 180;
        double f4_latitudineRadianti = (f4_latitudine * PIGRECO) / 180;
        double f4_longitudineRadianti = (f4_longitudine * PIGRECO) / 180;
        double f5_latitudineRadianti = (f5_latitudine * PIGRECO) / 180;
        double f5_longitudineRadianti = (f5_longitudine * PIGRECO) / 180;
        double p_latitudineRadianti = (p_latitudine * PIGRECO) / 180;
        double p_longitudineRadianti = (p_longitudine * PIGRECO) / 180;

        double distanzaUno = ((RAGGIO * Math.acos(Math.sin(laRadianti) * Math.sin(f1_latitudineRadianti) +
                Math.cos(laRadianti) * Math.cos(f1_latitudineRadianti) * Math.cos((loRadianti - f1_longitudineRadianti))))) * 1000;

        double distanzaDue = ((RAGGIO * Math.acos(Math.sin(laRadianti) * Math.sin(f2_latitudineRadianti) +
                Math.cos(laRadianti) * Math.cos(f2_latitudineRadianti) * Math.cos((loRadianti - f2_longitudineRadianti))))) * 1000;

        double distanzaTre = ((RAGGIO * Math.acos(Math.sin(laRadianti) * Math.sin(f3_latitudineRadianti) +
                Math.cos(laRadianti) * Math.cos(f3_latitudineRadianti) * Math.cos((loRadianti - f3_longitudineRadianti))))) * 1000;

        double distanzaQuattro = ((RAGGIO * Math.acos(Math.sin(laRadianti) * Math.sin(f4_latitudineRadianti) +
                Math.cos(laRadianti) * Math.cos(f4_latitudineRadianti) * Math.cos((loRadianti - f4_longitudineRadianti))))) * 1000;

        double distanzaCinque = ((RAGGIO * Math.acos(Math.sin(laRadianti) * Math.sin(f5_latitudineRadianti) +
                Math.cos(laRadianti) * Math.cos(f5_latitudineRadianti) * Math.cos((loRadianti - f5_longitudineRadianti))))) * 1000;

        double distanzaPunto = ((RAGGIO * Math.acos(Math.sin(laRadianti) * Math.sin(p_latitudineRadianti) +
                Math.cos(laRadianti) * Math.cos(p_latitudineRadianti) * Math.cos((loRadianti - p_longitudineRadianti))))) * 1000;

        DecimalFormat df1 = new DecimalFormat("0.00");
        double distanzaFilaUno = Double.parseDouble(df1.format(distanzaUno));
        double distanzaFilaDue = Double.parseDouble(df1.format(distanzaDue));
        double distanzaFilaTre = Double.parseDouble(df1.format(distanzaTre));
        double distanzaFilaQuattro = Double.parseDouble(df1.format(distanzaQuattro));
        double distanzaFilaCinque = Double.parseDouble(df1.format(distanzaCinque));
        double distanzaFilaPunto = Double.parseDouble(df1.format(distanzaPunto));

        if(distanzaFilaUno < distanzaFilaDue && distanzaFilaUno < distanzaFilaTre && distanzaFilaUno < distanzaFilaQuattro && distanzaFilaUno < distanzaFilaCinque){
            if(distanzaFilaUno > 110 && distanzaFilaUno < 300){
                textFila.setText("fila uno");
            } else if (distanzaFilaCinque > 300){
                textFila.setText("Ops!");
            } else {
                textFila.setText("1");
            }
        } else if (distanzaFilaDue < distanzaFilaUno && distanzaFilaDue < distanzaFilaTre && distanzaFilaDue < distanzaFilaQuattro && distanzaFilaDue < distanzaFilaCinque){
            if(distanzaFilaDue > 110 && distanzaFilaDue < 300){
                textFila.setText("fila due");
            } else if (distanzaFilaCinque > 300){
                textFila.setText("Ops!");
            } else {
                textFila.setText("2");
            }
        } else if (distanzaFilaTre < distanzaFilaUno && distanzaFilaTre < distanzaFilaDue && distanzaFilaTre < distanzaFilaQuattro && distanzaFilaTre < distanzaFilaCinque){
            if(distanzaFilaTre > 110 && distanzaFilaTre < 300){
                textFila.setText("fila tre");
            } else if (distanzaFilaCinque > 300){
                textFila.setText("Ops!");
            } else {
                textFila.setText("3");
            }
        } else if (distanzaFilaQuattro < distanzaFilaUno && distanzaFilaQuattro < distanzaFilaDue && distanzaFilaQuattro < distanzaFilaTre && distanzaFilaQuattro < distanzaFilaCinque){
            if(distanzaFilaQuattro > 110 && distanzaFilaQuattro < 300){
                textFila.setText("fila quattro");
            } else if (distanzaFilaCinque > 300){
                textFila.setText("Ops!");
            } else {
                textFila.setText("4");
            }
        } else if (distanzaFilaCinque < distanzaFilaUno && distanzaFilaCinque < distanzaFilaDue && distanzaFilaCinque < distanzaFilaTre && distanzaFilaCinque < distanzaFilaQuattro){
            if(distanzaFilaCinque > 110 && distanzaFilaCinque < 300){
                textFila.setText("fila cinque");
            } else if (distanzaFilaCinque > 300){
                textFila.setText("Ops!");
            } else {
                textFila.setText("5");
            }
        }


    } else {
        gps.showSettingsAlert();
    }


}

}