Im trying to create an app that will take the distance the phone moves with gps. My problem is that when I click the button b1 it wont this listener stops listening and wont take any further clicks.
What am I doing wrong? package com.HowMuchHowFar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.location.*;
public class HowMuchHowFarActivity extends Activity {
//declare variables
double lastlon,lastlat,curlon,curlat=0.0;
float total_distance=0;
float dist[]= new float[1];
boolean k2m_check=true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//create button
Button b1=(Button) findViewById(R.id.firstbtn);
System.out.println("hhhh");
//Open start activity
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//change to next activity
setContentView(R.layout.start);
//Create kilometer to meter button
Button k2m=(Button) findViewById(R.id.Kilometer);
//get access to text field
TextView display = (TextView) findViewById(R.id.display);
//start location manager
LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);
Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);
//Prepare for null value and force close
if(loc==null){
display.setText("No GPS location found");
}
else{
//set Current latitude and longitude
curlon=Double.valueOf((String.valueOf(loc.getLongitude())));
curlat=Double.valueOf((String.valueOf(loc.getLatitude())));
}
//Set the last latitude and longitude
lastlon=curlon;
lastlat=curlat;
//Start kilometer to meter button listener
k2m.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//start text check
//create button
Button k2m1=(Button) findViewById(R.id.Kilometer);
//get access to text field
TextView disp1 = (TextView) findViewById(R.id.display);
if(k2m_check==true){
//Change text
k2m1.setText("Meters");
onDestroy();
//startActivity();
//Set boolean for the next change
k2m_check=false;
disp1.setText(total_distance/1000+" Kilometers");
}
if(k2m_check==false){
//Change text
k2m1.setText("Kilometers");
//Set boolean for next change
k2m_check=true;
//Set display
disp1.setText(total_distance*1000+" Meters");
}//end text check
}
});//end k2m setonclicklistener
}
});
}//ends on create
LocationListener Loclist=new LocationListener(){
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
//start location manager
LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE);
TextView display1 = (TextView) findViewById(R.id.display);
//Create display for kilometer cost
TextView display_cpk = (TextView) findViewById(R.id.display_cpk);
TextView display_totalcost = (TextView) findViewById(R.id.display_totalcost);
//Get last location
Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);
//Request new location
lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);
//Get new location
Location loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);
//get the current lat and long
curlon=Double.valueOf((String.valueOf(loc.getLongitude())));
curlat=Double.valueOf((String.valueOf(loc.getLatitude())));
//Get distance between 2 locations put the distance into dist[0], in meters
Location.distanceBetween(lastlat,lastlon,curlat,curlon,dist);
//add the distance from this update to the total distance
total_distance=dist[0]+total_distance;
//Show distance travelled
display1.setText("Total distance "+total_distance+" Meters");
//Get cost per kilometer
CharSequence h=display_cpk.getText();
double mul=Double.parseDouble(h.toString());
//perform multipication and output cost
double cost=mul*(total_distance/1000);
display_totalcost.setText(String.valueOf(cost));
System.out.println(dist[0]);
System.out.println(mul);
System.out.println(total_distance/1000);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
};
}
It's because you're changing the content view, meaning any buttons on the previous layout are now invalid. If you're wanting to launch an activity, you should do so with startActivity()
or startActivityForResult()
rather than changing the content view. In all(Most? There may be exceptions) cases, you should only be calling setContentView()
one time per activity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.