-1

I'm getting the lat and lng from one URL. In my coding, it will be like, if(lat==1.3005060 && lng== 103.8745850) , it will open up up the next class, map.java. else, invalid map.

I've initially declare it as string to display it as text view. Next I've declare it as float in the onClick method, so that it can pass to google map. However, there is force close error occuring. Can someone guide me?

LogCat Error:

08-14 11:07:13.190: E/AndroidRuntime(457): FATAL EXCEPTION: main
08-14 11:07:13.190: E/AndroidRuntime(457): java.lang.IllegalStateException: Could not execute method of the activity
08-14 11:07:13.190: E/AndroidRuntime(457):  at android.view.View$1.onClick(View.java:2185)
08-14 11:07:13.190: E/AndroidRuntime(457):  at android.view.View.performClick(View.java:2585)
08-14 11:07:13.190: E/AndroidRuntime(457):  at android.view.View$PerformClick.run(View.java:9299)
08-14 11:07:13.190: E/AndroidRuntime(457):  at android.os.Handler.handleCallback(Handler.java:587)
08-14 11:07:13.190: E/AndroidRuntime(457):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 11:07:13.190: E/AndroidRuntime(457):  at android.os.Looper.loop(Looper.java:130)
08-14 11:07:13.190: E/AndroidRuntime(457):  at android.app.ActivityThread.main(ActivityThread.java:3691)
08-14 11:07:13.190: E/AndroidRuntime(457):  at java.lang.reflect.Method.invokeNative(Native Method)
08-14 11:07:13.190: E/AndroidRuntime(457):  at java.lang.reflect.Method.invoke(Method.java:507)
08-14 11:07:13.190: E/AndroidRuntime(457):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
08-14 11:07:13.190: E/AndroidRuntime(457):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
08-14 11:07:13.190: E/AndroidRuntime(457):  at dalvik.system.NativeStart.main(Native Method)
08-14 11:07:13.190: E/AndroidRuntime(457): Caused by: java.lang.reflect.InvocationTargetException
08-14 11:07:13.190: E/AndroidRuntime(457):  at java.lang.reflect.Method.invokeNative(Native Method)
08-14 11:07:13.190: E/AndroidRuntime(457):  at java.lang.reflect.Method.invoke(Method.java:507)
08-14 11:07:13.190: E/AndroidRuntime(457):  at android.view.View$1.onClick(View.java:2180)
08-14 11:07:13.190: E/AndroidRuntime(457):  ... 11 more
08-14 11:07:13.190: E/AndroidRuntime(457): Caused by: java.lang.NumberFormatException: latitude
08-14 11:07:13.190: E/AndroidRuntime(457):  at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:114)
08-14 11:07:13.190: E/AndroidRuntime(457):  at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:319)
08-14 11:07:13.190: E/AndroidRuntime(457):  at java.lang.Float.parseFloat(Float.java:323)
08-14 11:07:13.190: E/AndroidRuntime(457):  at com.example.fambond.SingleMenuItemActivity.onClick(SingleMenuItemActivity.java:75)
08-14 11:07:13.190: E/AndroidRuntime(457):  ... 14 more

part of my code for singlemenu.java

public void onClick(View v) {
         //TODO Auto-generated method stub
    float lat=Float.parseFloat(KEY_LATITUDE);
    float lng =Float.parseFloat(KEY_LONGITUDE);
    if(lat==1.3005060 && lng== 103.8745850){ 
    Log.d("coords", "lat: " + KEY_LATITUDE + " long: " + KEY_LONGITUDE);
    Intent intent = new Intent(SingleMenuItemActivity.this,map.class);
    intent.putExtra("MAP",KEY_LATITUDE.toString());
    intent.putExtra("MAP",KEY_LONGITUDE.toString());
    startActivity(intent);

    }
    else
    {
        Toast.makeText(SingleMenuItemActivity.this, "Invalid MAP", Toast.LENGTH_LONG).show();
    }
}
randomize
  • 137
  • 1
  • 2
  • 11
  • Which is line 75 in `SingleMenuItemActivity.java` ? Also which data type is `KEY_LATITUDE` variable ? – Shobhit Puri Aug 14 '13 at 03:17
  • line 75 is float lat=Float.parseFloat(KEY_LATITUDE); Key_latitude, I declare it as string, but in the website it is signed float. i'm able to retrieve it as textview for lat and lng, but couldnt pass it into google map – randomize Aug 14 '13 at 03:54
  • If `KEY_LATITUDE` is string containing the float value like `KEY_LATITUDE` = "1.3005060" then parsing should be okay. But iuf is a string then why are you using `KEY_LATITUDE.toString()` etc? – Shobhit Puri Aug 14 '13 at 04:04
  • then, how should I convert it instead? initially, it is declare at string – randomize Aug 14 '13 at 04:08
  • 1
    Err, in your other question you have `static final float KEY_LATITUDE = "latitude";` and `static final float KEY_LONGITUDE = "longitude";`. Which is totally wrong. – dmon Aug 14 '13 at 04:08
  • so, I shouldn't put them in? – randomize Aug 14 '13 at 05:08

1 Answers1

0

The problem seems to be that you are comparing floats with == sign. The correct way to test floats for 'equality' is:

if( (Math.abs(lat-1.3005060) < epsilon) && (Math.abs(lng-103.8745850) < epsilon) ){
}

where epsilon is a very small number like 0.00000001, depending on the desired precision.

See What's wrong with using == to compare floats in Java? and Manipulating and comparing floating points in java question for more.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124