Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6741835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:46:21+00:00 2026-05-26T11:46:21+00:00

Im trying to create an app that will take the distance the phone moves

  • 0

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

    }

};

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:46:21+00:00Added an answer on May 26, 2026 at 11:46 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a C# form app that will allow me to use
I am trying to create a small web app that will allow users to
I am trying to create an app that will display articles from an online
I am trying to create an app that will obtain the users location via
I am trying to create an android app that will connect to various web
I'm trying to create a windows form app that will be able to handle
I am trying to create an app that will give users the option to
I am trying to create an app that will help to find nearby places
I am trying to create a web app that will size itself to fill
I'm trying to create coloumns of text for a C# app that will be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.