(Solve)Error in Calculate Distance program...

Put your problem here if it does not fit any of the other categories.

(Solve)Error in Calculate Distance program...

Postby rakesh_mehta » Tue Oct 21, 2008 2:06 pm

Hello All..

I have problem in my application.. I want to calculate the distance between two static point... Here is my code:

import java.sql.Date;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TryVersion extends Activity {
private static final long WAIT_TIME=100;
private TextView latitudeView;
private TextView longitudeView;
private TextView altitudeView;
private TextView speedView;
private TextView bearingView;
private TextView timeView;
private TextView distanceView;
private EditText lat1Text;
private EditText lon1Text;
private EditText lat2Text;
private EditText lon2Text;
private LocationManager manager;
private Location loc,l1,l2;
private SimpleDateFormat dateFormat;
ProgressDialog dialog;
boolean gpsEnabled;

final Handler handler=new Handler();
private final Runnable updateLocation=new Runnable()
{
public void run()
{
trackGps();
}
};

final Runnable startTracking=new Runnable()
{
public void run()
{
dialog.dismiss();
trackGps();

}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
findViews();
setupListener();


}
@Override
protected void onResume()
{
super.onResume();
if(gpsEnabled)
{
trackGps();
}
}
@Override
protected void onPause()
{
super.onPause();
handler.removeCallbacks(updateLocation);
}

private void findViews()
{
latitudeView=(TextView)findViewById(R.id.latitude);
longitudeView=(TextView)findViewById(R.id.longitude);
altitudeView=(TextView)findViewById(R.id.altitude);
speedView=(TextView)findViewById(R.id.speed);
bearingView=(TextView)findViewById(R.id.bearing);
timeView=(TextView)findViewById(R.id.time);
distanceView=(TextView)findViewById(R.id.distance);
lat1Text=(EditText)findViewById(R.id.lat1);
lon1Text=(EditText)findViewById(R.id.lon1);
lat2Text=(EditText)findViewById(R.id.lat2);
lon2Text=(EditText)findViewById(R.id.lon2);
}

private void setupListener()
{
Button startGpsButton=(Button)findViewById(R.id.start);
startGpsButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
if(!gpsEnabled)
{
startGps();
}
}
});


Button stopGpsButton=(Button)findViewById(R.id.stop);
stopGpsButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
gpsEnabled=false;
}
});

Button calcDistanceButton=(Button)findViewById(R.id.calculate);
calcDistanceButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
calculateDistance();
}
});
}
void startGps()
{
System.out.println("hello1");
gpsEnabled=true;
dialog=ProgressDialog.show(TryVersion.this, null, "Initiazing...", true, false);
System.out.println("hello2");
Thread t=new Thread()
{
public void run()
{
manager.getProvider("gps");
handler.post(startTracking);
}
};
System.out.println("hello3");
t.start();
}
void trackGps()
{
if(!gpsEnabled)
{
return;
}
System.out.println("hello4");

System.out.println("hello41");

//double altitude=loc.getAltitude();
//System.out.println("loc"+loc.getAltitude());
double latitude=loc.getLatitude();
System.out.println("hello42");
double longitude=loc.getLongitude();
System.out.println("hello43");
float speed=loc.getSpeed();
float bearing=loc.getBearing();
long time=loc.getTime();
System.out.println("hello5");
//altitudeView.setText(Double.toString(altitude));
latitudeView.setText(Double.toString(latitude));
longitudeView.setText(Double.toString(longitude));
speedView.setText(Double.toString(speed));
bearingView.setText(Float.toString(bearing));
timeView.setText(dateFormat.format(new Date(time)));
handler.postDelayed(updateLocation,TryVersion.WAIT_TIME);
}

void calculateDistance()
{
try
{



System.out.println("hello6");
l1.setLatitude(Double.parseDouble(lat1Text.getText().toString()));

l1.setLongitude(Double.parseDouble(lon1Text.getText().toString()));

l2.setLatitude(Double.parseDouble(lat2Text.getText().toString()));
l2.setLongitude(Double.parseDouble(lon2Text.getText().toString()));
distanceView.setText(Float.toString(l1.distanceTo(l2)));
}
catch(Exception e)
{
distanceView.setText("Invalid Input");

}
}

}


But when i Click on the Calculate button there is an error message print..
Like null pointer exception...
Last edited by rakesh_mehta on Wed Oct 22, 2008 7:25 am, edited 1 time in total.
Rakesh
User avatar
rakesh_mehta
Experienced Developer
Experienced Developer
 
Posts: 73
Joined: Tue Oct 14, 2008 6:46 am
Location: Ahmedabad(India)

Top

Postby rakesh_mehta » Wed Oct 22, 2008 7:24 am

finally hear is the right code for caluculate the distance between two static point

void calculateDistance()
{
try
{



String distance="";
System.out.println("1");
Location l1=new Location(distance);
System.out.println("22");
l1.setLatitude(Double.parseDouble(lat1Text.getText().toString()));
System.out.println("2");
l1.setLongitude(Double.parseDouble(lon1Text.getText().toString()));
System.out.println("3");
Location l2=new Location(distance);
l2.setLatitude(Double.parseDouble(lat2Text.getText().toString()));
l2.setLongitude(Double.parseDouble(lon2Text.getText().toString()));
distanceView.setText(Float.toString(l1.distanceTo(l2)));
return;
}
catch(Exception e)
{
distanceView.setText("Invalid Input");
Rakesh
User avatar
rakesh_mehta
Experienced Developer
Experienced Developer
 
Posts: 73
Joined: Tue Oct 14, 2008 6:46 am
Location: Ahmedabad(India)

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 26 guests